我开发了一个带有两个选项卡的选项卡式应用程序,比如Audio和Video。
在方向更改时,我使标签指向设备的左侧。
但是在更改(我给的orientation
)时会变成空白。activity
textview
我希望activity
显示在orientation change
.
我将android:configChanges="screenSize|orientation"
活动元素放入清单文件中,因为我不希望重新创建选项卡活动。
任何人都可以发布解决此问题的方法吗?
公共类 MainTabActivity 扩展 TabActivity 实现
TabHost.TabContentFactory {
TabHost tabHost;
Intent intent;
Intent intent2;
Resources res;
Configuration cfg;
boolean hor;
boolean ver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate Maintab");
res = getResources();
cfg = res.getConfiguration();
hor = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE;
ver = cfg.orientation == Configuration.ORIENTATION_PORTRAIT;
if (hor) {
setContentView(R.layout.main_view_land);
tabHost = getTabHost();
TabWidget tw = tabHost.getTabWidget();
tw.setOrientation(LinearLayout.VERTICAL);
intent = new Intent(this, Activity1.class);
intent2 = new Intent(this, Activity2.class);
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator(
createIndicatorViewLand(tabHost, "Audio",
getResources()
.getDrawable(R.drawable.icon1)))
.setContent(intent));
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator(
createIndicatorViewLand(tabHost, "Video",
getResources()
.getDrawable(R.drawable.icon2)))
.setContent(intent2));
tabHost = getTabHost();
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_sellected);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_default);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_default);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(R.drawable.tab_sellected);
}
});
} else if (ver) {
setContentView(R.layout.main_view);
tabHost = getTabHost();
intent = new Intent(this, Activity1.class);
intent2 = new Intent(this, Activity2.class);
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator(
createIndicatorView(tabHost, "Audio",
getResources()
.getDrawable(R.drawable.icon1)))
.setContent(intent));
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator(
createIndicatorView(tabHost, "Video",
getResources()
.getDrawable(R.drawable.icon2)))
.setContent(intent2));
tabHost = getTabHost();
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_sellected);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_default);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_default);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(R.drawable.tab_sellected);
}
});
}
}
private View createIndicatorView(TabHost tabHost, CharSequence label,
Drawable icon) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tabIndicator = inflater.inflate(R.layout.tab_indicator,
tabHost.getTabWidget(), false);
final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(label);
final ImageView iconView = (ImageView) tabIndicator
.findViewById(R.id.icon);
iconView.setImageDrawable(icon);
return tabIndicator;
}
private View createIndicatorViewLand(TabHost tabHost, CharSequence label,
Drawable icon) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tabIndicator = inflater.inflate(R.layout.tab_indicator_land,
tabHost.getTabWidget(), false);
final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(label);
final ImageView iconView = (ImageView) tabIndicator
.findViewById(R.id.icon);
iconView.setImageDrawable(icon);
return tabIndicator;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("onCOnfig MainTab");
res = getResources();
cfg = res.getConfiguration();
hor = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE;
ver = cfg.orientation == Configuration.ORIENTATION_PORTRAIT;
if (hor) {
System.out.println("onCOnfig Horizontal");
setContentView(R.layout.main_view_land);
tabHost = getTabHost();
TabWidget tw = tabHost.getTabWidget();
tw.setOrientation(LinearLayout.VERTICAL);
// intent = new Intent(this, Activity1.class);
// intent2 = new Intent(this, Activity2.class);
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator(
createIndicatorViewLand(tabHost, "Audio",
getResources()
.getDrawable(R.drawable.icon1)))
.setContent(intent));
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator(
createIndicatorViewLand(tabHost, "Video",
getResources()
.getDrawable(R.drawable.icon2)))
.setContent(intent2));
tabHost = getTabHost();
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_sellected);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_default);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_default);
}
tabHost.getTabWidget()
.getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(
R.drawable.audio_video_sellected);
}
});
} else if (ver) {
System.out.println("onCOnfig Vertical");
setContentView(R.layout.main_view);
tabHost = getTabHost();
// intent = new Intent(this, Activity1.class);
// intent2 = new Intent(this, Activity2.class);
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator(
createIndicatorView(tabHost, "Audio",
getResources()
.getDrawable(R.drawable.icon1)))
.setContent(intent));
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator(
createIndicatorView(tabHost, "Video",
getResources()
.getDrawable(R.drawable.icon2)))
.setContent(intent2));
tabHost = getTabHost();
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_sellected);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_default);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_default);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(R.drawable.tab_sellected);
}
});
}
}
@Override
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return tv;
}
}