使用FragmentTabHost时,布局里FragmentTabHost、FrameLayout、TabWidget是组合使用的。
默认id分别为
android:id="@android:id/tabhost"
android:id="@android:id/tabcontent"
android:id="@android:id/tabs"
使用自定义的布局时,需要隐藏显示。
先看界面布局内容activity_main2.xml
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 默认布局组件,设置为隐藏 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- 默认装饰物对象,设置为隐藏 -->
<TabWidget
android:background="@layout/shape"
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
<!-- 设置底部位置显示,水平方向,垂直居中 -->
<!-- 默认垂直radion排列 -->
<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_widget_background"
android:padding="2dip"
android:orientation="horizontal"
>
<RadioButton
style="@style/tab_item_background"
android:drawableTop="@drawable/tab_icon1"
android:id="@+id/radioButton1"
android:text="主页"/>
<RadioButton
style="@style/tab_item_background"
android:drawableTop="@drawable/tab_icon2"
android:id="@+id/radioButton2"
android:text="搜索"/>
<RadioButton
style="@style/tab_item_background"
android:drawableTop="@drawable/tab_icon3"
android:id="@+id/radioButton3"
android:text="设置"/>
</RadioGroup>
</android.support.v4.app.FragmentTabHost>
引用到了styles.xml中的tab_item_backroud样式
<!-- 文本样式 -->
<style name="tab_item_text_style">
<item name="android:textSize">10.0dip</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:singleLine">true</item>
</style>
<style name="tab_item_background">
<item name="android:textAppearance">@style/tab_item_text_style</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- 选中与未选中背景 -->
<item name="android:background">@drawable/selector_tab_background2</item>
<!-- 取消勾选按钮 -->
<item name="android:button">@null</item>
<item name="android:drawablePadding">3.0dip</item>
<!-- 宽度均衡,填充底部布局 -->
<item name="android:layout_weight">1.0</item>
</style>
现在看java代码
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Switch;
public class MainActivity2 extends FragmentActivity {
String[] tabs = new String[]{"tab1","tab2","tab3"};
Class[] cls = new Class[]{Fragment1.class,Fragment2.class,Fragment3.class};
//定义全局tabs管理对象
private FragmentTabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//包含fragment组件的activity,需继承FragmentActivity
setContentView(R.layout.activity_main2);
//1.获取tabhost对象
tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
//指定显示布局
tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//2.循环创建tab添加只tabhost,创建时设置Indicator,名称
for (int i = 0; i < tabs.length; i++) {
//最后一个参数为绑定对象参数
tabhost.addTab(tabhost.newTabSpec(tabs[i]).setIndicator(tabs[i]),cls[i],null);
}
//3.获取RadioGroup对象
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
//4.注册监听事件
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//传递id参数
switch (checkedId) {
case R.id.radioButton1:
//更换frameLayout布局显示内容
tabhost.setCurrentTabByTag(tabs[0]);
break;
case R.id.radioButton2:
tabhost.setCurrentTabByTag(tabs[1]);
break;
case R.id.radioButton3:
tabhost.setCurrentTabByTag(tabs[2]);
break;
}
}
});
//5.初始化勾选第一个
((RadioButton) radioGroup.getChildAt(0)).toggle();
}
}
这里如果未调用tabhost.setup方法,会出现空指针错误。
运行后,如图
后面将学习页面左右滑动切换布局。