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

来源:互联网 发布:友窝能看m3u8源码 编辑:程序博客网 时间:2024/05/22 12:46
有时候经常在一个界面中要包含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);//开始设置tabHosttabHost.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 0