FragmentTabHost使用总结


一、使Activity继承FragmentActivityAppCompatActivity

AppCompatActivity也是继承自FragmentActivity,所以也可以继承它)

 

 

二、布局文件一定要这样写,尤其是<android.support.v4.app.FragmentTabHost />里面的内容

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <!-- TabHost调用setUp()方法时,其布局参数就是此FrameLayoutID-->

    <FrameLayout

        android:id="@+id/real_tab_content"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1">

    </FrameLayout>

 

    <!--存放TabFragmentTabHost-->

    <android.support.v4.app.FragmentTabHost

        android:id="@+id/tab_host"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@android:color/white">

        <!--官方要求写法-->

        <FrameLayout

            android:id="@android:id/tabcontent"//一定要这样写

            android:layout_width="0dp"

            android:layout_height="0dp"

            android:layout_weight = "0" >

       </FrameLayout>

    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

 

三、初始化Tab

FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.tab_host);

mTabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content);

四、初始化Indicator(为了使代码看起来简洁,可以将其封装)

(1)添加tab_indicator布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    android:layout_gravity="center"

    android:paddingTop="3dp"

    android:paddingBottom="3dp">

 

    <ImageView

        android:id="@+id/tab_icon"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

 

    <TextView

        android:id="@+id/text_indicator"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="2dp"

        android:textColor="@color/selector_tab_text"/>

</LinearLayout>

(2)在程序中初始化Indicator

private View buildIndicator() {

LayoutInflater mInflater = LayoutInflater.from(this);

View view = mInflater.inflate(R.layout.tab_indicator, null);

 

ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);

TextView textView = (TextView) view.findViewById(R.id.text_indicator);

 

imageView.setBackgroundResource(R.drawable.selector_icon);

textView.setText(“首页”);

//此处“首页”设置的是Tab上显示的文字,和下面newTabSpec()设置的“首页”不同

return view;

}

 

五、初始化Tab

TabHost.TabSpec mTabSpec = mTabHost.newTabSpec(“首页”); 

/此处“首页”String是设置的TAG

View view = inflater.inflate(R.Layout.tab_indicator); //Indicator布局填充进来作为Tab的布局

mTabSpec.setIndicator(view);

mTabHost.addTab(mTabSpec, HomeFragment, null); //将对应的每一个tabFragment添加进来



六、设置TabHost显示属性

 

//去除Tab间的分割线

mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

//默认显示第一个Tab及其对应的Fragment

mTabHost.setCurrentTab(0);

 

七、关于动态设置Tab中图片、字体的颜色

图片不同状态下切换,如selector_icon.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_focused="true" android:drawable="@mipmap/tab_person_selected" />

   <item android:state_selected="true" android:drawable="@mipmap/tab_person_selected" />

   <item android:drawable="@mipmap/tab_person_normal" />

</selector>

字体颜色一定要在res文件中的color文件中新建.xml文件,如selector_tab_text.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="#0262B2" />

    <item android:state_active="true" android:color="#0262B2" />

    <item android:state_selected="false" android:color="#E4E7E8" />

    <item android:state_active="false" android:color="#E4E7E8" />

</selector>

 

八、封装

在添加IndicatorTab时,会有多个(4~5)要添加,防止程序冗长繁杂,可以考虑将要添加的Tab定义为一个类,其中成员变量有:

private int title;

private int icon;

private Class fragment;

然后在带有着三个参数的Constructor对其进行初始化,并引入GetterSetter
在主程序中将Tab作为泛型传入List<Tab> (mTabs),可以一起添加。

buildIndicator()方法也可以传入Tab,方便操作。最后将其返回的View对象

for(Tab tab :mTabs) {

TabHost.TabSpec mTabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));

mTabSpec.setIndicator(buildIndicator(tab));

mTabHost.addTab(mTabSpec, tab.getFragment(), null);

        }

 

此外,也可以将要添加的title(TextView.setText(),String类型)、fragment(Fragment.class,Class类型)和icon(ImageView.setBackgroundResourceint类型,R.drawable.**))放入数组,这样在for循环中可以一起添加,方便很多,省去重复代码。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值