Android TabLayout设置选中状态标题字体大小,粗细

 
  
 
  
-------------------布局文件-----------------
<android.support.design.widget.TabLayout
    android:layout_marginLeft="17dp"
    android:layout_marginRight="16.8dp"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    app:tabGravity="fill"
    app:tabIndicatorColor="@color/button_bule"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="#333333"
    app:tabTextAppearance="@style/TabLayoutTextSize"
    app:tabTextColor="#2A2A2A"
    android:id="@+id/mytab"
    >android.support.design.widget.TabLayout>

<style name="TabLayoutTextSize">
    <item name="android:textSize">16spitem>
style>

<style name="TabLayoutTextStyle">
    <item name="android:textStyle">bolditem>
style>
-------------------代码展示-------------------
 
  
myTab = (TabLayout) view.findViewById(R.id.mytab);
//默认字体大小粗细
 
  
TextView title = (TextView)(((LinearLayout) ((LinearLayout) myTab.getChildAt(0)).getChildAt(0)).getChildAt(1));
title.setTextSize(18);
title.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
myTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
         vp.setCurrentItem(tab.getPosition());
        TextView title = (TextView)(((LinearLayout) ((LinearLayout) myTab.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
        title.setTextSize(18);
        title.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        vp.setCurrentItem(tab.getPosition());
        TextView title = (TextView)(((LinearLayout) ((LinearLayout) myTab.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
        title.setTextSize(16);
        title.setTextAppearance(getActivity(),Typeface.NORMAL);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
});

注意:

调用setOnTabSelectedListener点击菜单时没有切换fragment,不监听则正常。查看源码发现OnTabSelectedListener中的onTabSelected方法有个默认实现:

@Override
public void onTabSelected(TabLayout.Tab tab) {
     vp.setCurrentItem(tab.getPosition());
}
  • 1
  • 2
  • 3
  • 4

所以自己监听OnTabSelectedListener事件时需要在onTabSelected方法中调用ViewPager的setCurrentItem方法。


你可能感兴趣的:(Android,开发)