TabLayout sets the selected font size and thickness

https://blog.csdn.net/psjx0127/article/details/100661344 A
recent project has a requirement, that is, the selected item of the title needs to be bolded and enlarged, because the project uses TabLayout and TabLayout does not have corresponding attribute support. I have to find a way by myself!
TabLayout can customize the UI of the selected item, so you can start from this aspect. The
final solution is to add a listener to TabLayout, and customize the selected item UI in the listener. The code and layout files are as follows

mTabLayout.addOnTabSelectedListener(new ZTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(ZTabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(FinancialMainActivity.this, R.style.TabLayoutTextSize);
            }

            @Override
            public void onTabUnselected(ZTabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(FinancialMainActivity.this, R.style.TabLayoutTextSize_two);
            }

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

            }
        });	
        
		//监听一定要在setupWithViewPager方法之前添加,
        mTabLayout.setupWithViewPager(mViewPager);

The layout files used are as follows
 

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:textColor="@color/color_white"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"
    />

Select the style to bold and enlarge the font, write in style
 

 <style name="TabLayoutTextSize">
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">18sp</item>
    </style>

Default font
 

  <style name="TabLayoutTextSize_two">
        <item name="android:textSize">@dimen/font16</item>
        <item name="android:textStyle">normal</item>
    </style>

 

Guess you like

Origin blog.csdn.net/az44yao/article/details/113107490