先看一下效果:
TabHost是整个Tab的容器,包括两部分:
TabWidget和
FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。
TabHost的二种实现方式:
第一种:继承TabActivity
第二种:只是单纯的继承Activity类
下面是实现的效果:
第一种:继承TabActivity
1、如果我们使用extendsTabAcitivty,如同ListActivity,TabHost必须设置为 @android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent
布局文件 activity_main.xml
<LinearLayout xmlns: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:visibility="visible"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dp"
android:layout_weight="1.0"
android:background="@android:color/white">
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
下面是继承TabActivity
public class MainActivity extends TabActivity{
TabHosttabHost ;
RadioGroupradioGroup;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 换为其他
activity_main.xml
继承Activity类
public class MainActivity extends ActivityGroup{
TabHost tabHost ;
RadioGroupradioGroup;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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://nodebook.info/book/view?bid=5354725719980e913e9be3f1
http://www.apkbus.com/android-82426-1-1.html
http://www.apkbus.com/android-720-1-1.html
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0102/2894/fe1c3013-7a80-3073-93a4-e85ebda8bab2-thumb.png)
- 大小: 11.1 KB
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0102/2900/b7ffaefc-c41c-3fb4-867f-6d529a5ae7dc-thumb.png)
- 大小: 30.3 KB
分享到:
相关推荐
Android TabHost ListView初学者的实例代码
android Tabhost使用Demo
androidTabhost的使用实例代码,仅供参考
android tabhost 动态添加 删除 排序 重命名
android tabhost 动态添加,删除,排序,重命名
android tabhost 动态添加,删除,排序,重命名
android TabHost分页 demo 一开始就能用
Android TabHost动态增加和删除
android TabHost(标签)的使用 简单实例。
Android Tabhost 实例demo 自定义Tabhost实现实例。
NULL 博文链接:https://technicalsearch.iteye.com/blog/1924614
Android TabHost TabWidget 切换卡
android TabHost全用法,欢迎下载学习交流
android TabHost 底部显示 实现了底部显示选项卡
android Tabhost 实现底部菜单效果 菜单的图片和文字可以同时显示且选中带效果
Android TabHost嵌套示例,像电脑端一样在屏幕上显示上下嵌套的TAB效果,单击后切换内容,在内容页上又重新显示二级的TAB,实现嵌套,如示例图所示,注意:对于TabHost、布局文件中必须包含: TabHost、TabWidget ...
android tabhost 动态添加 删除 排序 重命名
android TabHost使用详解 TabHost从布局到代码的使用步骤
android tabhost android tabhost
Android TabHost实现屏幕UI窗口的TAB切换功能,实现的过程:声明TabHost对象、取得TabHost对象,为TabHost添加标签,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),然后...