Android UI TabHost总结

TabHost 包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。

1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent
4、参考这儿:http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.aspx
先自定义一个xml文件:
Java代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">   
  6.     <LinearLayout   
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent">   
  10.     <FrameLayout   
  11.         android:id="@android:id/tabcontent"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="fill_parent"  
  14.          android:layout_weight="1.0"  
  15.         android:paddingBottom="53px"/>   
  16.     <TabWidget   
  17.         android:id="@android:id/tabs"  
  18.         android:layout_alignParentBottom="true"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="50px"    
  21.         android:visibility="gone"  
  22.         android:layout_weight="0.0"/>   
  23.         <RadioGroup   
  24.             android:gravity="center_vertical"  
  25.             android:orientation="horizontal"  
  26.             android:id="@+id/main_radio"  
  27.             android:background="@drawable/radiogroup_background"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="50dip"  
  30.             android:layout_gravity="bottom">   
  31.             <RadioButton   
  32.                 android:id="@+id/main_index_button"  
  33.                 android:layout_marginTop="1.0dip"  
  34.                 android:layout_marginRight="5dip"  
  35.                 android:text="@string/main_name"  
  36.                 android:drawableTop="@drawable/unistall"  
  37.                 style="@style/main_tab_bottom"  
  38.                 android:background="@drawable/radio_bg"/>   
  39.             <RadioButton   
  40.                 android:id="@+id/main_running_button"  
  41.                 android:layout_marginTop="1.0dip"  
  42.                 android:layout_marginRight="5dip"  
  43.                 android:text="@string/run_manager_name"  
  44.                 android:drawableTop="@drawable/unistall"  
  45.                 style="@style/main_tab_bottom"  
  46.                 android:background="@drawable/radio_bg"/>   
  47.             <RadioButton   
  48.                 android:id="@+id/main_uninstall_button"  
  49.                 android:layout_marginTop="1.0dip"  
  50.                 android:text="@string/uninstall_manager_name"  
  51.                 android:drawableTop="@drawable/unistall"  
  52.                 style="@style/main_tab_bottom"  
  53.                 android:background="@drawable/radio_bg"/>   
  54.         </RadioGroup>   
  55.     </LinearLayout>   
  56. </TabHost>  
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout
		android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
	<FrameLayout
		android:id="@android:id/tabcontent"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		 android:layout_weight="1.0"
		android:paddingBottom="53px"/>
	<TabWidget
		android:id="@android:id/tabs"
		android:layout_alignParentBottom="true"
		android:layout_width="fill_parent"
		android:layout_height="50px" 
		android:visibility="gone"
		android:layout_weight="0.0"/>
		<RadioGroup
			android:gravity="center_vertical"
			android:orientation="horizontal"
			android:id="@+id/main_radio"
			android:background="@drawable/radiogroup_background"
			android:layout_width="fill_parent"
			android:layout_height="50dip"
			android:layout_gravity="bottom">
			<RadioButton
				android:id="@+id/main_index_button"
				android:layout_marginTop="1.0dip"
				android:layout_marginRight="5dip"
				android:text="@string/main_name"
				android:drawableTop="@drawable/unistall"
				style="@style/main_tab_bottom"
				android:background="@drawable/radio_bg"/>
			<RadioButton
				android:id="@+id/main_running_button"
				android:layout_marginTop="1.0dip"
				android:layout_marginRight="5dip"
				android:text="@string/run_manager_name"
				android:drawableTop="@drawable/unistall"
				style="@style/main_tab_bottom"
				android:background="@drawable/radio_bg"/>
			<RadioButton
				android:id="@+id/main_uninstall_button"
				android:layout_marginTop="1.0dip"
				android:text="@string/uninstall_manager_name"
				android:drawableTop="@drawable/unistall"
				style="@style/main_tab_bottom"
				android:background="@drawable/radio_bg"/>
		</RadioGroup>
	</LinearLayout>
</TabHost>

为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下方。style="@style/main_tab_bottom"里面定义了样式文件。

接下来就是在activity中初始化并添加tabhost:
Java代码
  1. tabHost = (TabHost) findViewById(android.R.id.tabhost);   
  2.         tabHost.addTab(Constant.tabHost.newTabSpec("Main")   
  3.                 .setIndicator(getString(R.string.main_name),null)   
  4.                 .setContent(new Intent(this, Main.class)));   
  5.         tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")   
  6.                 .setIndicator(getString(R.string.run_manager_name),null)   
  7.                 .setContent(new Intent(this, RunManager.class)));   
  8.         tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager")   
  9.                 .setIndicator(getString(R.string.uninstall_manager_name),null)   
  10.                 .setContent(new Intent(this, UninstallManager.class)));  
tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.addTab(Constant.tabHost.newTabSpec("Main")
				.setIndicator(getString(R.string.main_name),null)
				.setContent(new Intent(this, Main.class)));
		tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")
				.setIndicator(getString(R.string.run_manager_name),null)
				.setContent(new Intent(this, RunManager.class)));
		tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager")
				.setIndicator(getString(R.string.uninstall_manager_name),null)
				.setContent(new Intent(this, UninstallManager.class)));


初始化每个RadioButton并为其添加setOnCheckedChangeListener事件,当点击相应的RadioButton时就可以通过setCurrentTabByTag()方法显示到当前页面。
 
  1. private void initRadios() {   
  2.         ((RadioButton) findViewById(R.id.main_index_button))   
  3.                 .setOnCheckedChangeListener(this);   
  4.         ((RadioButton) findViewById(R.id.main_running_button))   
  5.                 .setOnCheckedChangeListener(this);   
  6.         ((RadioButton) findViewById(R.id.main_uninstall_button))   
  7.                 .setOnCheckedChangeListener(this);   
  8.     }   
  9.        
  10.     @Override  
  11.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
  12.         if(isChecked){   
  13.             switch (buttonView.getId()) {   
  14.             case R.id.main_index_button:   
  15.                 tabHost.setCurrentTabByTag("Main");   
  16.                 break;   
  17.             case R.id.main_running_button:   
  18.                 tabHost.setCurrentTabByTag("RunManager");   
  19.                 break;   
  20.             case R.id.main_uninstall_button:   
  21.                 tabHost.setCurrentTabByTag("UninstallManager");   
  22.                 break;   
  23.             }   
  24.         }   
  25.     }  
private void initRadios() {
		((RadioButton) findViewById(R.id.main_index_button))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.main_running_button))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.main_uninstall_button))
				.setOnCheckedChangeListener(this);
	}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			switch (buttonView.getId()) {
			case R.id.main_index_button:
				tabHost.setCurrentTabByTag("Main");
				break;
			case R.id.main_running_button:
				tabHost.setCurrentTabByTag("RunManager");
				break;
			case R.id.main_uninstall_button:
				tabHost.setCurrentTabByTag("UninstallManager");
				break;
			}
		}
	}


小结:
1、在一个tabActivity里面嵌套一个tabAcitivity, 如果在子tabActivity里面显示AlertDialog、ProgressDialog的话,就会引发此错误:android.view.WindowManager$BadTokenException: Unable to add window

解决方法:
可以把创建dialog时传递的参数xxx.this改成this.getParent(),其中的xxx为Activity
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHostAndroid 应用程序中常用的选项卡控件,用于在多个选项卡之间切换。以下是 Android Studio 中使用 TabHost 控件的步骤: 1. 在布局文件中添加 TabHost 控件 ``` <TabHost android:id="@+id/tab_host" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加选项卡内容布局 --> <LinearLayout android:id="@+id/tab1_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 1" /> </LinearLayout> <LinearLayout android:id="@+id/tab2_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 2" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> ``` 2. 在 Java 代码中获取 TabHost 控件并设置选项卡 ```java TabHost tabHost = findViewById(R.id.tab_host); tabHost.setup(); // 添加选项卡 TabHost.TabSpec tab1 = tabHost.newTabSpec("Tab1"); tab1.setIndicator("Tab 1"); tab1.setContent(R.id.tab1_layout); tabHost.addTab(tab1); TabHost.TabSpec tab2 = tabHost.newTabSpec("Tab2"); tab2.setIndicator("Tab 2"); tab2.setContent(R.id.tab2_layout); tabHost.addTab(tab2); ``` 在以上代码中,我们首先获取 TabHost 控件,并通过 `setup()` 方法初始化。然后,我们使用 `newTabSpec()` 方法创建选项卡,并设置选项卡的标签和内容布局。最后,我们使用 `addTab()` 方法将选项卡添加到 TabHost 控件中。 以上就是使用 TabHost 控件的基本步骤。你可以根据自己的需要自定义选项卡的样式和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值