TabHost的使用方法(不继承TabActivity)

有时候经常在一个界面中要包含TabHost(选项卡),可以用<include>标签套入一个TabHost的界面

那么TabHost的界面就根据要求,填充一些交互界面,例如:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout              
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TabWidget               //id必须是@android:id/tabs
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <FrameLayout             //id必须是@android:id/tabcontent
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

	    //在FrameLayout添加要填充的每个界面,就是选项卡中每个选项卡包含的内容
	    //这里是表示有两个选项卡,每个选项卡中的界面包含的还是布局文件
            <include
                android:id="@+id/item1"
                layout="@layout/item1_layout" />
            <include
                android:id="@+id/item2"
                layout="@layout/item2_layout" />
        </FrameLayout>
    </LinearLayout>
</TabHost>
写到这里TabHost的布局文件就算是写完了
然后在Activity中获取TabHost组件,添加每个选项卡的图片或者名称

//获取TabHost对象
TabHost tabHost = (TabHost) findViewById(R.id.tab_test);
//开始设置tabHost
tabHost.setup();
//新建一个newTabSpec,设置标签(选项卡名称)和图标(setIndicator),设置内容(setContent)
tabHost.addTab(tabHost.newTabSpec("Test one").setIndicator("我是第一个选项卡",getResources().getDrawable(android.R.drawable.ic_menu_call)).setContent(R.id.item1));
tabHost.addTab(tabHost.newTabSpec("Test two").setIndicator("我是第二个选项卡",getResources().getDrawable(android.R.drawable.ic_menu_camera)).setContent(R.id.item2));
//设置TabHost的背景颜色
//tabHost.setBackgroundColor(Color.argb(150,22,70,150));
//设置TabHost的背景图片资源
//tabHost.setBackgroundResource(R.drawable.bg);

此方法Activity不用继承TabActivity

如果想将选项卡放在底部,可以将上边的TabHost中的FrameLayout和TabWidget交换一下位置,
然后将FrameLayout的weight设置成大于1的数,其他的不便即可,参照如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout              
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        <FrameLayout             //id必须是@android:id/tabcontent
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dip" 
	    android:layout_weight="1"  >

	    //在FrameLayout添加要填充的每个界面,就是选项卡中每个选项卡包含的内容
	    //这里是表示有两个选项卡,每个选项卡中的界面包含的还是布局文件
            <include
                android:id="@+id/item1"
                layout="@layout/item1_layout" />
            <include
                android:id="@+id/item2"
                layout="@layout/item2_layout" />
        </FrameLayout>

	<TabWidget               //id必须是@android:id/tabs
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</TabHost>



选项卡中包含的内容就是在<include>标签下包含的内容,自己相加什么就加什么吧,哈哈

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHost 是 Android 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值