很久没有应用了,东西都忘记了,总结出来,曾经的东西在捡起来一次,加强下记忆
上代码:
- 在XML的布局:
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroudColor">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/wirteColor"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/wirteColor">
<LinearLayout
android:id="@+id/line_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/line_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/line_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</TabHost>
- java类中的实现:
TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
//添加“主页”Tab到TabHost控件中
mTabHost.addTab(mTabHost.newTabSpec("home")//Tag
.setIndicator("HOME")//设置Tab标签和图标
.setContent(R.id.line_one)); //设置Tab内容
//添加“消息”Tab到TabHost控件中
mTabHost.addTab(mTabHost.newTabSpec("news")
.setIndicator("NEWS")
.setContent(R.id.line_two));
//添加“个人”Tab到TabHost控件中
mTabHost.addTab(mTabHost.newTabSpec("mine")
.setIndicator("MINE")
.setContent(R.id.line_three));
//设置当前默认显示第一个Tab
mTabHost.setCurrentTab(0);
//TabHost改变监听
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Log.d("===>>>onTabChanged",tabId.toString());
}
});
- 下来我们要创建属于三个界面的Fragment(
HomeFragment
.NewsFragment
,MineFragment
) - 最后分别添加到TabHost的tabcontent中,tabcontent里面有几个子空间,它就有几个tab
//home
HomeFragment nomeFragment= new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_one, homeFragment).commit();
//news
NewsFragment newsFragment = new NewsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_two, newsFragment ).commit();
//mine
MineFragment mineFragment= new MineFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_three, mineFragment).commit();
- 当然了Tab的标签和图标可以自定义一个View,通过
drawable
来设置每个Tab选中时的颜色等...(下面只试一个:在layout
下定义一个home_item.xml
)
<!-- === layout中 === -->
//只在布局中添加下面就行了,再去drawable创建选中时的字体颜色--home_item_selected
<TextView
android:textSize="@dimen/sp18"
android:textColor="@drawable/home_item_selected"
android:text="@string/string_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- === drawable中 === -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/appthemeColor" />
<item android:color="@color/textColor" />
</selector>
好了,就这么简单的完了,当然了,TabHost+Fragment的写法有好几种,我这里只用了最简单的这一种,当然,现在由于项目的需求,有些也用不上此方法,因为这个加载数据是一次性全部执行,三个Fragment的生命周期一起执行的