tablayout 选中加粗

activity_task_manager.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:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="ResourceName">

    <com.pingan.transportation.platform.driver.widget.DriverTitleBar
        android:id="@+id/task_manager_title_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_48"
        app:centerText="任务管理"
        app:leftImageResource="@mipmap/icon_back"
        app:leftImageSize="@dimen/dp_20"
        app:qrImageResource="@mipmap/icon_qrscan" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_1"
        android:background="#EBECF1" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/task_manager_tablayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        app:tabBackground="@android:color/white"
        app:tabIndicatorColor="@color/c_C9A15E"
        app:tabIndicatorHeight="@dimen/dp_2"
        app:tabMode="scrollable"
        app:tabPaddingEnd="-1dp"
        app:tabPaddingStart="-1dp"
        app:tabRippleColor="@android:color/transparent"
        app:tabSelectedTextColor="@color/c_C9A15E"
        app:tabTextAppearance="@style/TabLayoutTextSize"
        app:tabTextColor="#0000ff" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/task_manager_vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

tab_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_item_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="@string/app_name"
    android:textColor="@color/red"
    android:textSize="@dimen/sp_14" />

public class TaskManageActivity extends BaseDriverActivity {

    @BindView(R2.id.task_manager_title_bar)
    DriverTitleBar titleBar;
    @BindView(R2.id.task_manager_tablayout)
    TabLayout tabLayout;
    @BindView(R2.id.task_manager_vp)
    ViewPager viewPager;

    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private ArrayList<String> titleList = new ArrayList<>();

    private int index = 0;//第几个fragment
    public static final int TASK_ALL = 0;//全部
    public static final int TASK_PREPARE = 1;//待发车
    public static final int TASK_DEPARTURE = 2;//进行中
    public static final int TASK_FINISH = 3;//已完成
    public static final int TASK_CLOSE = 4;//已关闭
    public static final int TASK_OVERDUE = 5;//已过期

    @Override
    protected int getContentViewId() {
        return R.layout.activity_task_manager;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        super.onViewCreated(savedInstanceState);

        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                index = bundle.getInt("index", 0);
            }
        }

        fragmentList.add(TaskAllFragment.newInstance());
        fragmentList.add(TaskFragment.newInstance(TASK_PREPARE));
        fragmentList.add(TaskFragment.newInstance(TASK_DEPARTURE));
        fragmentList.add(TaskFragment.newInstance(TASK_FINISH));
        fragmentList.add(TaskFragment.newInstance(TASK_CLOSE));
        fragmentList.add(TaskFragment.newInstance(TASK_OVERDUE));
        titleList.add("全部");
        titleList.add("待发车");
        titleList.add("进行中");
        titleList.add("已完成");
        titleList.add("已关闭");
        titleList.add("已过期");
        TaskManageVpAdapter adapter = new TaskManageVpAdapter(getSupportFragmentManager(), this, fragmentList, titleList);
        viewPager.setAdapter(adapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView textView = (TextView) LayoutInflater.from(TaskManageActivity.this).inflate(R.layout.tab_item, null);
                textView.setTextColor(getResources().getColor(R.color.c_C9A15E));
                textView.setText(tab.getText());
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                tab.setCustomView(textView); 
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);//恢复到蓝色默认
            }

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

            }
        });

        tabLayout.setupWithViewPager(viewPager);

        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager.setCurrentItem(index);
        viewPager.setOffscreenPageLimit(5);
    }
}

上一篇:TvTabLayout 针对 Android Tv 端把原生的 tablayout 进行适配和改造


下一篇:Android项目开发之--------地铁时光机(一,搭建主框架)