Tab的用法

来源:互联网 发布:动态qos 知乎 编辑:程序博客网 时间:2024/05/24 05:18
本帖最后由 misskong 于 2011-12-21 14:13 编辑

tabHost往往被当作程序的通用框架入口,其主要的使用方式有两种: 1.继承TabActivity,结合对应的xml配置文件导入tab选项内容体   2.继承Activity,结合拥有TabHost标签的xml配置文件导入

对于1:

Java代码
  1. public class TabExdHostActivity extends TabActivity {

  2.         @Override
  3.         public void onCreate(Bundle savedInstanceState) {
  4.                 super.onCreate(savedInstanceState);
  5.                 TabHost myTabHost = this.getTabHost();
  6.                 LayoutInflater.from(this).inflate(R.layout.exmain, myTabHost.getTabContentView(), true);
  7.                 myTabHost.addTab(myTabHost.newTabSpec("选项卡1").setIndicator("选项卡1").setContent(R.id.tab1));
  8.                 myTabHost.addTab(myTabHost.newTabSpec("选项卡2").setIndicator("选项卡2").setContent(R.id.tab2));
  9.                 myTabHost.addTab(myTabHost.newTabSpec("选项卡3").setIndicator("选项卡3").setContent(R.id.tab3));
  10.         }
  11. }
复制代码
Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <LinearLayout android:id="@+id/tab1" android:layout_width="fill_parent"
  6.                 android:layout_height="fill_parent" android:orientation="vertical">
  7.                 <TextView android:id="@+id/view1" android:layout_width="wrap_content"
  8.                         android:layout_height="wrap_content" android:text="@string/textView_1" />
  9.         </LinearLayout>
  10.         
  11.         <LinearLayout android:id="@+id/tab2" android:layout_width="fill_parent"
  12.                 android:layout_height="fill_parent">
  13.                 <TextView android:id="@+id/view2" android:layout_width="wrap_content"
  14.                         android:layout_height="wrap_content" android:text="@string/textView_2" />
  15.         </LinearLayout>
  16.         
  17.         <LinearLayout android:id="@+id/tab3" android:layout_width="fill_parent"
  18.                 android:layout_height="fill_parent" android:orientation="vertical">
  19.                 <TextView android:id="@+id/view3" android:layout_width="wrap_content"
  20.                         android:layout_height="wrap_content" android:text="@string/textView_3" />
  21.         </LinearLayout>
  22.         
  23.         <LinearLayout android:id="@+id/tab4" android:layout_width="fill_parent"
  24.                 android:layout_height="fill_parent" android:orientation="vertical">
  25.                 <TextView android:id="@+id/view4" android:layout_width="wrap_content"
  26.                         android:layout_height="wrap_content" android:text="@string/textView_4" />
  27.         </LinearLayout>
  28. </LinearLayout>
复制代码
对于2:

Java代码
  1.         
  2. public void onCreate(Bundle savedInstanceState) {
  3.                 super.onCreate(savedInstanceState);
  4.                 setContentView(R.layout.main);
  5.                 tabhost = (TabHost) findViewById(R.id.tabhost);
  6.                 tabhost.setup();
  7.                 TabHost.TabSpec spec = tabhost.newTabSpec("tab1");
  8.                 spec.setContent(R.id.tab1);
  9.                 spec.setIndicator("主页");
  10.                 tabhost.addTab(spec);

  11.                 TabHost.TabSpec spec2 = tabhost.newTabSpec("tab2");
  12.                 spec2.setContent(R.id.tab2);
  13.                 spec2.setIndicator("主页2");
  14.                 tabhost.addTab(spec2);

  15.                 TabHost.TabSpec spec3 = tabhost.newTabSpec("tab3");
  16.                 spec3.setContent(R.id.tab3);
  17.                 spec3.setIndicator("主页3");
  18.                 tabhost.addTab(spec3);

  19.                 TabHost.TabSpec spec4 = tabhost.newTabSpec("tab4");
  20.                 spec4.setContent(R.id.tab4);
  21.                 spec4.setIndicator("主页4");
  22.                 tabhost.addTab(spec4);

  23.                 
  24.                 tabhost.setOnTabChangedListener(this);
  25.                 
  26.                 bt_cg = (Button) findViewById(R.id.bt_cg);
  27.                 bt_cg.setOnClickListener(new OnClickListener() {

  28.                         @Override
  29.                         public void onClick(View v) {
  30.                                 Intent intent = new Intent(TabHostActivity.this, TabExdHostActivity.class);
  31.                                 startActivity(intent);
  32.                         }
  33.                 });

  34.         }
复制代码
Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <TabHost android:id="@+id/tabhost" android:layout_width="match_parent"
  6.                 android:layout_height="match_parent">
  7.                 <LinearLayout android:layout_width="match_parent"
  8.                         android:id="@+id/linearLayout1" android:layout_height="match_parent"
  9.                         android:orientation="vertical">
  10.                         <TabWidget android:layout_width="match_parent"
  11.                                 android:orientation="vertical" android:layout_height="wrap_content"
  12.                                 android:id="@android:id/tabs"></TabWidget>
  13.                         <FrameLayout android:layout_width="match_parent"
  14.                                 android:layout_height="match_parent" android:id="@android:id/tabcontent">
  15.                                 <LinearLayout android:id="@+id/tab1"
  16.                                         android:layout_width="fill_parent" android:layout_height="fill_parent"
  17.                                         android:orientation="vertical">
  18.                                         <TextView android:id="@+id/view1" android:layout_width="wrap_content"
  19.                                                 android:layout_height="wrap_content" android:text="@string/textView_1" />
  20.                                         <Button android:id="@+id/bt_cg" android:layout_width="wrap_content"
  21.                                                 android:layout_height="wrap_content" android:text="chageTab" />
  22.                                 </LinearLayout>
  23.                                 <LinearLayout android:id="@+id/tab2"
  24.                                         android:layout_width="fill_parent" android:layout_height="fill_parent">
  25.                                         <TextView android:id="@+id/view2" android:layout_width="wrap_content"
  26.                                                 android:layout_height="wrap_content" android:text="@string/textView_2" />
  27.                                 </LinearLayout>
  28.                                 <LinearLayout android:id="@+id/tab3"
  29.                                         android:layout_width="fill_parent" android:layout_height="fill_parent"
  30.                                         android:orientation="vertical">
  31.                                         <TextView android:id="@+id/view3" android:layout_width="wrap_content"
  32.                                                 android:layout_height="wrap_content" android:text="@string/textView_3" />
  33.                                 </LinearLayout>
  34.                                 <LinearLayout android:id="@+id/tab4"
  35.                                         android:layout_width="fill_parent" android:layout_height="fill_parent"
  36.                                         android:orientation="vertical">
  37.                                         <TextView android:id="@+id/view4" android:layout_width="wrap_content"
  38.                                                 android:layout_height="wrap_content" android:text="@string/textView_4" />
  39.                                 </LinearLayout>
  40.                         </FrameLayout>

  41.                 </LinearLayout>
  42.         </TabHost>
  43. </LinearLayout>
复制代码
有时候需要tabhost里面的标签按钮置于底部,可以对xml文件进行如下修改:


Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <TabHost android:id="@+id/tabhost" android:layout_width="match_parent"
  6.                 android:layout_height="match_parent">
  7.                 <RelativeLayout android:layout_width="match_parent"
  8.                         android:id="@+id/linearLayout1" android:layout_height="match_parent"
  9.                         android:orientation="vertical">
  10.                         <TabWidget android:layout_width="match_parent"
  11.                             android:layout_alignParentBottom="true"
  12.                                 android:orientation="vertical" android:layout_height="wrap_content"
  13.                                 android:id="@android:id/tabs"></TabWidget>
  14.                         <FrameLayout android:layout_width="match_parent"
  15.                                 android:layout_height="match_parent" android:id="@android:id/tabcontent">
  16.                                 <LinearLayout android:id="@+id/tab1"
  17.                                         android:layout_width="fill_parent" android:layout_height="fill_parent"
  18.                                         android:orientation="vertical">
  19.                                         <TextView android:id="@+id/view1" android:layout_width="wrap_content"
  20.                                                 android:layout_height="wrap_content" android:text="@string/textView_1" />
  21.                                         <Button android:id="@+id/bt_cg" android:layout_width="wrap_content"
  22.                                                 android:layout_height="wrap_content" android:text="chageTab" />
  23.                                 </LinearLayout>
  24.                                 <LinearLayout android:id="@+id/tab2"
  25.                                         android:layout_width="fill_parent" android:layout_height="fill_parent">
  26.                                         <TextView android:id="@+id/view2" android:layout_width="wrap_content"
  27.                                                 android:layout_height="wrap_content" android:text="@string/textView_2" />
  28.                                 </LinearLayout>
  29.                                 <LinearLayout android:id="@+id/tab3"
  30.                                         android:layout_width="fill_parent" android:layout_height="fill_parent"
  31.                                         android:orientation="vertical">
  32.                                         <TextView android:id="@+id/view3" android:layout_width="wrap_content"
  33.                                                 android:layout_height="wrap_content" android:text="@string/textView_3" />
  34.                                 </LinearLayout>
  35.                                 <LinearLayout android:id="@+id/tab4"
  36.                                         android:layout_width="fill_parent" android:layout_height="fill_parent"
  37.                                         android:orientation="vertical">
  38.                                         <TextView android:id="@+id/view4" android:layout_width="wrap_content"
  39.                                                 android:layout_height="wrap_content" android:text="@string/textView_4" />
  40.                                 </LinearLayout>
  41.                         </FrameLayout>

  42.                 </RelativeLayout>
  43.         </TabHost>
  44. </LinearLayout>
复制代码
效果如图: 1.png 
原创粉丝点击