android学习笔记31:TabHost

本文介绍如何使用Android中的TabHost组件来创建类似电话应用中已接、未接和呼出电话的标签式界面。通过定义不同的LinearLayout和TextView组件,为每个标签页提供特定的内容,并利用TabActivity设置TabHost。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用TabHost就可以实现我们打电话时经常使用的那个界面了,就是已接来电、未接来电那个。


定义TabHost控件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 定义第一个标签页的内容 -->

    <LinearLayout
        android:id="@+id/tab01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="胡锦涛 - 2012/1/12"
            android:textSize="11pt" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="温家宝 - 2012/1/18"
            android:textSize="11pt" />
    </LinearLayout>
    <!-- 定义第二个标签页的内容 -->

    <LinearLayout
        android:id="@+id/tab02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="孙燕姿 - 2012/1/12"
            android:textSize="11pt" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="梁静茹- 2012/1/20"
            android:textSize="11pt" />
    </LinearLayout>
    <!-- 定义第三个标签页的内容 -->

    <LinearLayout
        android:id="@+id/tab03"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:textSize="11pt" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bill- 2011/09/19"
            android:textSize="11pt" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="李开复 - 2011/10/12"
            android:textSize="11pt" />
    </LinearLayout>

</TabHost>

继承TabActivity,不过这个类目前已经过时了,现在android的提倡使用FragMents。

public class TabHostTest extends TabActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		TabHost tabHost = getTabHost();
		//设置使用TabHost布局
		LayoutInflater.from(this).inflate(R.layout.main,
				tabHost.getTabContentView(), true);
		//添加第一个标签页
		tabHost.addTab(tabHost.newTabSpec("tab1")
			.setIndicator("已接电话", getResources().getDrawable(R.drawable.take))
			.setContent(R.id.tab01)); 
		//添加第二个标签页
		tabHost.addTab(tabHost.newTabSpec("tab2")
			//在标签标题上放置图标
			.setIndicator("呼出电话" 
				, getResources().getDrawable(R.drawable.callout))
			.setContent(R.id.tab02)); 
		//添加第三个标签页
		tabHost.addTab(tabHost.newTabSpec("tab3")
			.setIndicator("未接电话", getResources().getDrawable(R.drawable.untake))
			.setContent(R.id.tab03)); 		
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值