Android屏幕在方向更改时变为空白

时间:2013-04-22 09:00:11

标签: android android-activity android-orientation

我开发了一个标签式应用程序,其中包含两个标签,例如音频视频

在方向改变时,我将标签定位在设备的左侧。

但是在更改orientation时,activity(我给了textview)变为空白。

我希望activity上显示orientation change

我将android:configChanges="screenSize|orientation"放入清单文件中的activity元素,因为我不希望重新创建标签活动。

有人可以发布一种解决这个问题的方法吗?

public class MainTabActivity extends TabActivity implements
        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; }

}

0 个答案:

没有答案