Tabhost以及其用法

TabHost是一种非常使用的组件,TabHost可以方便的在窗口上放置多个标签页,每个标签页相当于或得了一个与外部容器相同大小的组件摆放区域。

与TabHost 结合使用的组件

  TabWidget:代表选项卡的标签条。

   TabSpec:代表选项卡的一个Tab页面。

创建添加选项卡的方法:

 newTabSpec():创建选项卡

 addTab():添加选项卡

使用TabHost的步骤:

  1. 在界面布局中定义TabHost组件

  2. Activity继承TabActivity

  3. 调用TabActivity的getTabHost()方法获取TabHost

  4. 通过TabHost对象的方法来创建添加选项卡。


  5. <? xml  version = "1.0"  encoding = "utf-8" ?>
  6. < LinearLayout
  7.      xmlns:android = "http://schemas.android.com/apk/res/android"
  8.      android:layout_width = "match_parent"  android:layout_height = "match_parent"
  9.     android:orientation = "vertical"
  10.      >
  11.    
  12. < TabHost
  13.      android:layout_width = "match_parent"  android:layout_height = "match_parent"
  14.      android:id = "@android:id/tabhost"
  15.      android:layout_weight = "1"
  16.      > <!--引用android系统已有的id-->
  17.      < LinearLayout
  18.          android:layout_width = "match_parent"
  19.          android:layout_height = "match_parent"
  20.          android:orientation = "vertical"
  21.          >
  22.          < TabWidget
  23.              android:id = "@android:id/tabs"
  24.              android:layout_width = "match_parent"
  25.              android:layout_height = "wrap_content" ></ TabWidget >//代表选项卡的标签条
  26.          < FrameLayout
  27.              android:id = "@android:id/tabcontent"
  28.              android:layout_width = "match_parent"
  29.              android:layout_height = "match_parent" >
  30.              <!--定义第一个标签页的内容-->
  31.              < LinearLayout
  32.                  android:id = "@+id/tab01"
  33.                  android:orientation = "vertical"
  34.                  android:layout_width = "fill_parent"
  35.                  android:layout_height = "fill_parent" >
  36.                  < TextView
  37.                      android:layout_width = "fill_parent"
  38.                      android:layout_height = "fill_parent"
  39.                      android:text = "消息"
  40.                      android:textSize = "30sp"
  41.                      />
  42.              </ LinearLayout >
  43.              < LinearLayout
  44.                  android:id = "@+id/tab02"
  45.                  android:orientation = "vertical"
  46.                  android:layout_width = "fill_parent"
  47.                  android:layout_height = "fill_parent" >
  48.                  < TextView
  49.                      android:layout_width = "fill_parent"
  50.                      android:layout_height = "fill_parent"
  51.                      android:text = "赛事"
  52.                      android:textSize = "30sp"
  53.                      />
  54.              </ LinearLayout >
  55.              < LinearLayout
  56.                  android:id = "@+id/tab03"
  57.                  android:orientation = "vertical"
  58.                  android:layout_width = "fill_parent"
  59.                  android:layout_height = "fill_parent" >
  60.                  < TextView
  61.                      android:layout_width = "fill_parent"
  62.                      android:layout_height = "fill_parent"
  63.                      android:text = "我的页面"
  64.                      android:textSize = "30sp"
  65.                      />
  66.              </ LinearLayout >
  67.          </ FrameLayout >
  68.  
  69.      </ LinearLayout >
  70. </ TabHost >
  71. </ LinearLayout >
  72. 注意:必须使用这样的,其他的id是错误的
  73. android:id="@android:id/tabhost"
  74.  android:id="@android:id/tabs"
  75.  android:id="@android:id/tabcontent"
  76.  引用android系统已有的id
  77. public  class  MainActivity  extends  TabActivity{ //Activity继承TabActivity
  78.  
  79.      @Override
  80.      protected  void  onCreate(Bundle savedInstanceState) {
  81.          super .onCreate(savedInstanceState);
  82.          setContentView(R.layout.main_activity);
  83.          //调用TabActivity的getTabHost()方法获取TabHost
  84.          TabHost tabHost=getTabHost();
  85.          //创建第一个Tab页
  86.          TabHost.TabSpec tab1=tabHost.newTabSpec( "tab1" )
         
  87.                 .setIndicator( "赛事" ) //设置标题
  88.                  .setContent(R.id.tab01);
  89.          //添加第一个tab页
  90.          tabHost.addTab(tab1);
  91.          TabHost.TabSpec tab2=tabHost.newTabSpec( "tab2" )
  92.                  .setIndicator( "消息" ) //设置标题
  93.                  .setContent(R.id.tab02);
  94.          //添加第一个tab页
  95.          tabHost.addTab(tab2);
  96.  
  97.          TabHost.TabSpec tab3=tabHost.newTabSpec( "tab3" )
  98.                  .setIndicator( "我" ) //设置标题
  99.                  .setContent(R.id.tab03);
  100.          //添加第一个tab页
  101.          tabHost.addTab(tab3);
  102.      }
  103. }



你可能感兴趣的:(id,tabhost)