android - TabLayout 中选定的自定义选项卡文本颜色

标签 android android-layout tabs android-tablayout

我正在尝试创建自定义选项卡布局,因为我需要在 TextView 旁边设置徽章计数器。正如文档中所述,我已将 id 设置为 @android:id/text1

当我的自定义选项卡被选中时,TextView 颜色不会自动更改。如何以正确和干净的方式实现它?

正确选择的默认标签:

enter image description here

错误选择的自定义选项卡(文本为灰色但应为白色):

enter image description here

代码

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);

最佳答案

其实还是用选择器比较好

这是一个使用 Kotlin 和带有 tabLayout 的最新 viewPager2 的示例(基于 Google 的示例 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>

关于android - TabLayout 中选定的自定义选项卡文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44107655/

相关文章:

java - LinearLayout 不测量内部的 TextView

android - 使用选择器使按钮背景透明

android - 如何使用android中的任何布局将两个 TextView 与背景水平对齐?

flutter - Flutter:如何根据选择的选项卡在滑动时更改选项卡栏之外的文本

visual-studio-2010 - VS2010 : How to prevent changing a Horizontal Tab Group to a Vertical Tab Group when restarting?

android - Android 中的自定义选择器 View

javascript - phonegap 3.5 播放本地音频文件

android - 在多模块 Android 项目中找不到 Kotlin typealias

android - 如何在 cordova webview 中加载第三方非安全图像?

jquery - 选项卡选择事件不适用于 jQuery 1.8.3