Android TabLayout自定义指示器

Android TabLayout自定义指示器

导入tablayout包

 implementation 'com.google.android.material:material:1.2.1'

xml文件(activity_main.xml)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    android:layout_height="match_parent">
    
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:tabGravity="fill"
        app:tabIndicator="@drawable/shape_indicator"
        app:tabIndicatorColor="#FFDD00"
        app:tabIndicatorFullWidth="false"
        app:tabMaxWidth="0px"
        app:tabMode="scrollable"
        app:tabPaddingEnd="6dp"
        app:tabPaddingStart="6dp"
        app:tabRippleColor="#00000000"
        app:tabSelectedTextColor="#000000"
        app:tabTextAppearance="@style/tabLayoutStyle"
        app:tabTextColor="#999999" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

指示器 shape_indicator.xml (文件位置放置:res/drawable)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="center_horizontal">
        <shape>
            <solid android:color="#FFDD00"/>
            <corners android:radius="4dp" />
            <size
                android:width="26dp"
                android:height="4dp" />
        </shape>
    </item>
</layer-list>

样式 tabLayoutStyle (文件位置放置:res/values/styles.xml)
这里设置tab的字体大小为16dp

<resources xmlns:tools="http://schemas.android.com/tools">
    <!--...其他的style-->
 	<style name="tabLayoutStyle">
        <item name="android:textSize">16dp</item>
    </style>
</resources>    

java代码设置

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tabLayout  = findViewById(R.id.tab_layout);
        List<String> titles = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        titles.add("全部");
        titles.add("北京望京");
        titles.add("上海");
        titles.add("广州张杰演唱会");
        titles.add("深圳");
        titles.add("长沙世界之窗");
        titles.add("武汉");
        titles.add("南京");
        for (int i = 0; i < titles.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles.get(i)));
            fragments.add(MainFragment.newInstance(titles.get(i),""));
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String s = tab.getText().toString();
                SpannableString ss = new SpannableString(s);
                ss.setSpan(new StyleSpan(Typeface.BOLD),0,s.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ss.setSpan(new AbsoluteSizeSpan(18,true),0,s.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                tab.setText(ss);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                String s = tab.getText().toString();
                SpannableString ss = new SpannableString(s);
                ss.setSpan(new StyleSpan(Typeface.NORMAL),0,s.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ss.setSpan(new AbsoluteSizeSpan(16,true),0,s.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                tab.setText(ss);
            }

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

            }
        });
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
               return titles.get(position);
            }
        });

        tabLayout.setupWithViewPager(viewPager);
    }
}

你可能感兴趣的:(android)