虽然tabhost已经被官方废弃了,但是用还是能用,其搭配activity或者fragment都能很好的实现页面的切换
使用条件:MainAcitivy必须继承自TabActivity,通过调用getTabHost();方法获取TabHost对象
TabHost常用组件:
TabWidget:该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡
FrameLayout:该组件中定义的子组件是TabHost中每个页面显示的选项卡
1.<tabhost>根布局标签
其中的id 需要引用 android的自带id : android:id="@android:id/tabhost" ;
getHost()获取前提 : 设置了该id之后, 在Activity界面可以使用 getHost(), 获取这个TabHost 视图对象;
2.TabWidget标签
该组件是选项卡切换按钮, 通过点击该组件可以切换选项卡---必须添加的组件,但是可以不使用,采用自定义的选项组件
这个组件的id要设置成android的自带id : android:id="@android:id/tabs"
该组件与FrameLayout组件是TabHost组件中必备的两个组件
如果想要将按钮放到下面, 可以将该组件定义在下面, 但是注意:FrameLayout要设置android:layout_widget = "1"
如果想要设置该按钮组件的大小, 可以设置该组件与FrameLayout组件的权重
3.FrameLayout
该组件中定义的子组件是TabHost中每个页面显示的选项卡, 可以将TabHost选项卡显示的视图定义在其中
这个组件的id要设置成android的自带的id: android:id="@android:id/tabcontent"
注意:三个部分tabhost,tabwidget,framelayout都是使用的系统指定的id,不能自己设定
贴上布局代码:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dp"
android:layout_weight="1.0" >
</FrameLayout>
<!--因为不适用,所以直接gone掉,下面的linearLayout为自定义的按键-->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="0.0dp"
android:layout_weight="0.0"
android:visibility="gone" />
<LinearLayout
android:id="@+id/mine_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@null" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom|center"
android:orientation="vertical" >
<RadioButton
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/message_infor_0"
android:gravity="center"
android:text="first" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@null" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom|center"
android:orientation="vertical" >
<RadioButton
android:id="@+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/message_inform_1"
android:gravity="center"
android:text="second" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@null" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom|center"
android:orientation="vertical" >
<RadioButton
android:id="@+id/third"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/message_infor_0"
android:gravity="center"
android:text="third" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@null" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom|center"
android:orientation="vertical" >
<RadioButton
android:id="@+id/forth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/message_inform_1"
android:gravity="center"
android:text="forth" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</TabHost>
使用tabWidget作为选项卡的布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_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:orientation="horizontal"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/alwayswet"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:scaleType="fitXY"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/alwayswet"/>
</LinearLayout>
<LinearLayout
android:id="@+id/isanimal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:scaleType="fitXY"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/isanimal"/>
</LinearLayout>
<LinearLayout
android:id="@+id/nezha"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:scaleType="fitXY"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/nazha"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
在MainActivity中调用:
TabHost localTab =getTabHost();
自定义选项卡:
private TabHost.TabSpec buildTabSpec(String tag,String name,final Intent content){
return mTabHost.newTabSpec(tag).setContent(content).setIndicator(name);
}
localTab.addTab(buildTabSpec("tag", "name", Intent));
说明:intent为MainActivity 跳往目标class的intent
继承并重写监听:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switch(buttonView.getId()){
case R.id.first:
mTabHost.setCurrentTabByTag("fi");
second.setChecked(false);
third.setChecked(false);
forth.setChecked(false);
break;
case R.id.second:
mTabHost.setCurrentTabByTag("se");
first.setChecked(false);
third.setChecked(false);
forth.setChecked(false);
break;
case R.id.third:
mTabHost.setCurrentTabByTag("th");
second.setChecked(false);
first.setChecked(false);
forth.setChecked(false);
break;
case R.id.forth:
mTabHost.setCurrentTabByTag("fo");
second.setChecked(false);
third.setChecked(false);
first.setChecked(false);
break;
}
}
}
即可;
使用tabWidget作为选项卡的调用代码:
TabHost tabHost = getTabHost();
TabSpec page1 = tabHost.newTabSpec("tab1")
.setIndicator("叫兽")
.setContent(R.id.isanimal);
tabHost.addTab(page1);
TabSpec page2 = tabHost.newTabSpec("tab2")
.setIndicator("老湿")
.setContent(R.id.alwayswet);
tabHost.addTab(page2);
TabSpec page3 = tabHost.newTabSpec("tab3")
.setIndicator("哪吒")
.setContent(R.id.nezha);
tabHost.addTab(page3);