在TabLayout中选择的自定义标签文本颜色 [英] Selected custom tab text color in TabLayout

查看:308
本文介绍了在TabLayout中选择的自定义标签文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建自定义标签布局,因为我需要在 TextView 旁边设置徽章计数器.正如我在文档中提到的,我已将id设置为 @android:id/text1 .

I'm trying to create custom tab layout because I need to set badge counter next to TextView. I've set id to @android:id/text1 as it's mentioned in doc.

选择我的自定义标签后,TextView颜色不会自动更改.如何以正确和干净的方式实现它?

When my custom tab is selected, TextView color isn't changed automatically. How to achieve it in correct and clean way?

正确选择的默认标签:

错误选择的自定义标签(文本为灰色,但应为白色):

Wrong selected custom tab (text is grey but should be white):

代码

PagerAdapter adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(2);
if (tab != null) { 
    tab.setCustomView(R.layout.tab_proposed_rewards);
}

布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        android:textAppearance="@style/TextAppearance.Design.Tab"/>

    <TextView
        android:id="@+id/indicator"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/background_indicator"
        android:gravity="center"
        android:lines="1"/>

</LinearLayout>

修改

答案:

tab.setCustomView(R.layout.tab_proposed_rewards);
ColorStateList textColor = tabLayout.getTabTextColors();
TextView textView = (TextView) tab.getCustomView().findViewById(android.R.id.text1);
textView.setTextColor(textColor);

推荐答案

实际上,最好使用选择器.

Actually, it's better to use a selector.

以下是使用Kotlin和带有tabLayout的最新viewPager2的示例(基于Google的示例此处):

Here's a sample using Kotlin and the latest viewPager2 with tabLayout (based on Google's sample here):

        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            val tabView = LayoutInflater.from(this).inflate(R.layout.tab_with_badge, tabLayout, false)
            tabView.textView.text = "item$position" 
            tabView.badgeTextView.text = position.toString()
            tab.customView = tabView
        }.attach()

tab_with_badge.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"
    android:orientation="horizontal">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textColor="@color/tab_color_selector" tools:text="@tools:sample/lorem" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/badgeTextView" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:background="@drawable/badge_background" tools:text="12" />

</LinearLayout>

tab_color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#f00" android:state_pressed="true" />
    <item android:color="#0f0" android:state_selected="true" />
    <item android:color="#00f" />
</selector>

badge_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <padding
        android:left="4dp"
        android:right="4dp" />
    <solid android:color="@color/tab_color_selector" />
    <corners android:radius="8dp" />
</shape>

这篇关于在TabLayout中选择的自定义标签文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆