android TabLayout设置选中标签字体加粗功能

实现 TabLayout 选中tab标签字体加粗功能如下:

xml文件中定义:

 <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                app:tabIndicatorColor="@color/orange"
                app:tabIndicatorHeight="2dp"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/_333333"
                app:tabTextColor="@color/_4c4c4c"
                app:tabTextAppearance="@style/TabLayoutStyle"/>
<style name="TabLayoutStyle">
        <item name="android:textSize">18sp</item>
        <item name="android:textStyle">bold</item>
    </style>

java文件中设置:

//引用tablayout
ArrayList<String> tabList = new ArrayList<>();
tabList.add("tab1");

tabList.add("tab2");
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(tabList.get(0));
        tabLayout.addTab(tabLayout.newTab().setText(tabList.get(1)));

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        updateTabTextView(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()), true);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabView(tab, true);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabView(tab, false);
            }

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

            }
        });
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView textView = (TextView) view.findViewById(R.id.tab_item_textview);
        textView.setText(tabList.get(currentPosition));
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {

        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setText(tab.getText());
        }
    }
<?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tab_item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="@color/333333"
        />

</LinearLayout>

 

 

 

posted @ 2017-11-20 16:45  你好and程序员  阅读(18515)  评论(0编辑  收藏  举报