Android用户界面开发:TabHost

TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。
TabHost的二种实现方式:
第一种:继承TabActivity
第二种:只是单纯的继承Activity类
下面是实现的步骤:
第一种:继承TabActivity
1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent
布局文件
main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
   <TabHost android:id="@android:id/tabhost"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
       <LinearLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="vertical">
           <TabWidget android:id="@android:id/tabs"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"/>
           <FrameLayout android:id="@android:id/tabcontent"
               android:layout_width="fill_parent"
               android:background="@android:color/white">
           </FrameLayout>
       </LinearLayout>
   </TabHost>
</LinearLayout>
下面是继承TabActivity
public class MainActivity extends TabActivity{
    TabHosttabHost ;
   @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
       tabHost =getTabHost();
      tabHost.addTab(tabHost.newTabSpec("trends").setIndicator("动态").setContent(newIntent(MainActivity.this,TrendsActivity.class)));
      tabHost.addTab(tabHost.newTabSpec("groups").setIndicator("群组").setContent(newIntent(MainActivity.this,GroupsActivity.class)));
    }
}
第二种:不继承任何类在代码里加载
布局文件  和上面一样 只是TabHost 的id 换为其他
main.xml
继承Activity类
public class MainActivity extends ActivityGroup{
    TabHost tabHost ;
   @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
tabHost = (TabHost)findViewById(R.id.m_tabhost);
       //如果通过findViewById得到TabHost一定要调用 TabHost.setup();
        LocalActivityManagerlocalAcManager = new LocalActivityManager(MainActivity.this,true);
      localAcManager.dispatchCreate(savedInstanceState);
tabHost.setup(localAcManager);
      tabHost.addTab(tabHost.newTabSpec("trends").setIndicator("动态").setContent(newIntent(MainActivity.this,TrendsActivity.class)));
      tabHost.addTab(tabHost.newTabSpec("groups").setIndicator("群组").setContent(newIntent(MainActivity.this,GroupsActivity.class)));
    }
}
两种介绍完了。但系统提供的TabHost不好看,修改很难...
看了下别人的  大概是这样修改的,就第二种方法而言,其他不改,把TabWidget空间变为不可见,然后自己弄些控件替代。
  <TabWidget
               android:id="@android:id/tabs"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:visibility="gone"/>

参考链接:http://www.apkbus.com/forum.php?mod=viewthread&tid=82426

原文地址:https://www.cnblogs.com/klcf0220/p/3258404.html