一、什么是TabHost。
Android 里面的TabHost就是选项卡,看下图(新浪微博界面):
至于选项卡有什么好处或者用途,我想代码哥们都知道吧,我就不多说了。
二、在Android里面如何实现TabHost
有两种方式可以实现。
1、继承TabActivity,然后用getTabHost()方法获取TabHost,最后在布局文件中定义各个Tab选项卡添加到TabHost中
2、不继承TabActivity,然后在布局文件中定义TabHost,最后讲各个Tab选项卡添加到TabHost中
总结以上两种方式为两步:
①:获取TabHost对象
②:把Tab添加到TabHost中。
我们先看第一种实现:
①:布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 第一个选项卡面板 -->
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 面板中只有一个TextView-->
<TextView android:id="@+id/V1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>
</LinearLayout>
<!-- 第二个选项卡面板 -->
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 面板中只有一个TextView-->
<TextView android:id="@+id/V2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>
</LinearLayout>
</LinearLayout>
②:Activity
package com.droidstouch.tabhost;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
/**
* @author <a href="http://bbs.droidstouch.com">Touch Android</a>
*
*/
public class Demo2Activity extends TabActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// this.setContentView(R.layout.demo2); // 注意不要加上此行代码
//获取到TabHost对象
TabHost tabHost =this.getTabHost();
//把我们的布局文件添加到tabHost 的FrameLayout下面
LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true);
// 下面定义了两个选项卡
//获取一个新的TabHost.TabSpec,并关联到当前tab host
//参数:所需的选项卡标签
TabSpec pSpec = tabHost.newTabSpec("parent");
// 参数一:选项卡上的文字,参数二:选项卡的背景图片
pSpec.setIndicator("父类", this.getResources().getDrawable(R.drawable.f_root));
//设置选项卡内容
pSpec.setContent(R.id.tab1);
TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类", this.getResources().getDrawable(R.drawable.f_sub));
subSpec.setContent(R.id.tab2);
// 将选项卡添加到TabHost中
tabHost.addTab(pSpec);
tabHost.addTab(subSpec);
}
}
第二种方式
①:布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义TabHost ,自定义的TabHost一定得包含TabWidget 和 FrameLayout,
并且 TabWidget 的ID一定是@android:id/tabs
FrameLayout 的Id一定是@android:id/tabcontent
-->
<TabHost android:id="@+id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义TabWidget,此控件 必须和TabHost一起使用 -->
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- 定义FrameLayout-->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>
<TextView android:id="@+id/txtV2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="http://bbs.droidstouch.com"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
②:Activity:
package com.droidstouch.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Dome1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo1);
//从布局文件中 获取到TabHost
TabHost tabHost = (TabHost) this.findViewById(R.id.tabs);
//安装TabHost
tabHost.setup();
// 下面定义两个选项卡
//获取一个新的TabHost.TabSpec,并关联到当前tab host
//参数:所需的选项卡标签
TabSpec pSpec = tabHost.newTabSpec("parent");
pSpec.setIndicator("父类", this.getResources().getDrawable(R.drawable.f_root));
pSpec.setContent(R.id.txtV1);
TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类", this.getResources().getDrawable(R.drawable.f_root));
subSpec.setContent(R.id.txtV2);
//添加选项卡到TabHost中
tabHost.addTab(pSpec);
tabHost.addTab(subSpec);
}
}
分享到:
相关推荐
androidTabhost的使用实例代码,仅供参考
android Tabhost使用Demo
Android TabHost ListView初学者的实例代码
android tabhost 动态添加 删除 排序 重命名
Android Tabhost 实例demo 自定义Tabhost实现实例。
Android 嵌套TabHost示例. 两层TabHost嵌套,实现的效果类似网页上的二级菜单栏。
android tabhost 动态添加,删除,排序,重命名
Android TabHost动态增加和删除
android TabHost(标签)的使用 简单实例。
android TabHost分页 demo 一开始就能用
NULL 博文链接:https://technicalsearch.iteye.com/blog/1924614
android tabhost 动态添加,删除,排序,重命名
android TabHost全用法,欢迎下载学习交流
Android TabHost TabWidget 切换卡
android Tabhost 实现底部菜单效果 菜单的图片和文字可以同时显示且选中带效果
android TabHost 底部显示 实现了底部显示选项卡
Android实现tabHost控件自定义页签页面源码
android tabhost 动态添加 删除 排序 重命名
本人亲测,非常好用的tabhost代码,已经运行了,非常好用,适合新手小白
类似微信主界面,使用tabhost界面切换