UI布局思路
1,上面是一个Fragment
2,下面是一个FragmentTabHost,但是中间有一个被ImageView覆盖
FrameLayout
weight="1"
height="0dp"
RelativeLayout
FragmentTabHost
ImageView
centerInparent="true"
Activity中的思路
1,初始化FragmentTabHost
2,判断版本号如果大于10,去除分隔线
3,定义一个内部类MainTabInfo,用于存放 文本 图标 Fragment的字节码类
4,创建一个泛型MainTabInfo的集合,将需要的数据存放在集合中
5,遍历集合
1,使用FragmentTabHost创建一个指定的tab标签
2,加载一个tab标签的视图
3,给视图中的TextView绑定数据,文本和图标
4,mFragmentTabHost添加标签
1,可以给Fragment传递参数Bundle
UI代码
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.app.FragmentTabHost
android:id="@+id/fragment_tab_host"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.app.FragmentTabHost>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/btn_quickoption_selector"/>
</RelativeLayout>
Activity中的代码
“`
/**
* 用于初始化FragmentTabhost
* 1,初始化FragmentTabHost
* 2,创建一个包含Fragment,文字,图标的集合
* 3,创建一个FragmentTabHost的标签
* 4,设置标签的视图
* 5,传递到Fragment的参数Bundle
* 6,添加标签到FragmentTabHost
*/
private void initFragmentTabHost() {
//1,初始化FragmentTabhost
mFragmentTabHost.setup(this,getSupportFragmentManager(),R.id.fragment_container);
if(Build.VERSION.SDK_INT > 10){
//去除分隔线
mFragmentTabHost.getTabWidget().setShowDividers(0);
}
//2,创建一个包含Fragment 文字 图标的集合
List<MainTabInfo> list = new ArrayList<>();
list.add(new MainTabInfo("综合",R.drawable.tab_icon_new, NewsTabFragment.class));
list.add(new MainTabInfo("动弹",R.drawable.tab_icon_tweet,TweetTabFragment.class));
list.add(new MainTabInfo("",R.drawable.tab_icon_new,FindTabFragment.class));
list.add(new MainTabInfo("发现",R.drawable.tab_icon_explore,FindTabFragment.class));
list.add(new MainTabInfo("我",R.drawable.tab_icon_me,MeTabFragment.class));
for (int i = 0; i < list.size(); i++) {
//3,使用FragmentTabHost创建一个标签
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(list.get(i).title);
//4,加载一个视图
View tabView = View.inflate(MainActivity.this, R.layout.main_tab_item, null);
//获取里面的文本控件
TextView textView = (TextView) tabView.findViewById(R.id.tab_title);
//给文本控件设置标题和图标
textView.setText(list.get(i).title);
textView.setCompoundDrawablesWithIntrinsicBounds(0,list.get(i).resId,0,0);
//5,给指定标签添加视图
tabSpec.setIndicator(tabView);
//6,向Fragment传递参数
Bundle bundle = new Bundle();
bundle.putString("args","我是第"+i+"个参数");
//7,FragmentTabHost添加指定的标签
mFragmentTabHost.addTab(tabSpec,list.get(i).fragmentClass,bundle);
//8,隐藏第三个视图
if(i ==2 ){
tabView.setVisibility(View.INVISIBLE);
}
}
main_tab_item中的布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="@color/primarybar_txt"
android:id="@+id/tab_title"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:visibility="gone"
android:text="10"
android:textColor="#f00"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>