在 TabLayout 中选择自定义选项卡文本颜色

2024-05-04

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

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

正确选择默认选项卡:

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

Code

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

Layout

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

Edit

Answer:

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 https://github.com/android/views-widgets-samples):

        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>

徽章_背景.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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 TabLayout 中选择自定义选项卡文本颜色 的相关文章

随机推荐

  • 如何在两个不同的视图控制器之间传递信息?

    这是一个简单的问题 我有 2 个不同的视图控制器 每个视图控制器都有自己的数据存储在其 m 文件中 我想取一个值 例如 一个整数值 int i 3 在 ViewController 1 中声明并将其传递给 ViewController 2
  • 如何使用 BeautifulSoup4 获取
    标记之前的所有文本

    我正在尝试为我的应用程序抓取一些数据 我的问题是我需要一些 HTML 代码如下 tr td This a class tip info href blablablablabla is a first a sentence br This a
  • pandas 从日期时间转换为整数时间戳

    考虑 python 中的 pandas 数据框有一个名为time整数类型 我可以将其转换为datetime按照以下说明进行格式化 df time pandas to datetime df time unit s 所以现在该列有如下条目 2
  • Linq:将扁平结构转换为分层结构

    转换平面结构最简单且有效的方法是什么 object rawData new object A1 B1 C1 A1 B1 C2 A2 B2 C3 A2 B2 C4 more 变成层次结构 class X public X Cs new Lis
  • lambda 函数的代码覆盖率

    我有以下带有 lambda 函数的代码 obj method param gt code here 如何通过测试覆盖 lambda 函数中的代码 您可以使用反射 但这可能容易出错并且适得其反 我建议你调用使用 lambda 的方法
  • 在 Windows 窗体应用程序中捕获 MonthCalendar 控件的双击

    如何捕获 System Windows Forms MonthCalendar 控件的双击事件 我尝试过使用 MouseDown 的 MouseEventArgs Clicks 属性 但它始终为 1 即使我双击也是如此 请注意 MonthC
  • 从后台弹出时片段的 onResume() 不会被调用

    您好 我正在开发 Android 应用程序 我正在使用它 我正在使用单个Activity和3个碎片 所以考虑我有 3 个片段 A B C 当我从 A 切换到 B 时 我添加Fragment现在 当我从 C 单击返回时 它会显示 B 并且 B
  • HTML5 应用程序缓存与浏览器缓存

    当前浏览器中实现了 applicationCache 我的应用程序缓存清单文件更改版本号 然后触发 applicationCache 更新事件 强制浏览器从服务器下载清单文件中提到的新资源 假设我已经在这些资源上配置了远期到期标头 这些文件
  • 通过 facebook api 在 facebook feed 中发布 swf

    我正在使用下面的数组 feeddata array type gt flash method gt stream publish display gt iframe link gt https developers facebook com
  • 最近点对算法

    我目前正在致力于用 C 实现最接近的点对算法 也就是说 给定一个点列表 x y 找到具有最小欧氏距离的点对 我对此进行了研究 我对算法的理解如下 如果我错了 请纠正我 将点数组从中间拆分 递归地找到左半部分和右半部分距离最小的点对 按 y
  • 相机姿态估计(OpenCV PnP)

    我正在尝试使用网络摄像头从具有已知全球位置的四个基准点的图像中获取全局姿态估计 我检查了许多 stackexchange 问题和一些论文 但似乎无法得到正确的解决方案 我得到的位置数字是可重复的 但与相机移动绝不成线性比例 仅供参考 我正在
  • 如何隐藏数据透视项标题?

    我希望有一个具有 PivotItems 但没有横向数据透视项标题文本的数据透视控件 它是横向模式下的画廊 当恢复为纵向时 它应该再次显示 PivotItems 标题 解决办法就是制作文字PivotItem Header 不好 因为标题文本占
  • 使用CSS按比例调整图像大小? [复制]

    这个问题在这里已经有答案了 有没有办法仅使用 CSS 按比例调整 缩小 图像大小 我正在使用 JavaScript 方式 但只是想看看这是否可以用 CSS 实现 要使用 CSS 按比例调整图像大小 img resize width 540p
  • 您使用什么工具进行静态代码分析? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 这个问题 https stackoverflow com questions 20702 whats youra good limit for
  • 将 List 保存到 ASP.NET 中的会话

    购物车项目保存在 SQL 数据库中 我想将所有 CartItems 放在一个 List 中并转移到 Instance Items The Instance变量正在保存到会话中 代码如下 public class ShoppingCart p
  • 如何离线分析使用 pstats.dump_stats(filename) 创建的文件?

    我基本上做了以下工作 import cProfile pstats StringIO pr cProfile Profile pr enable my code did something pr disable s StringIO Str
  • Google Chrome:同时“平滑”scrollIntoView() 与更多元素不起作用

    在谷歌浏览器中 element scrollIntoView with behavior smooth 不能同时在多个容器上工作 一旦在一个容器上触发平滑滚动 第二个容器就会停止滚动 在Firefox中 这个问题不存在 两个容器可以同时滚动
  • 从命令输出中设置 GDB 中的环境变量

    我试图在挑战中利用缓冲区溢出 缓冲区从环境变量中获取其值 在 GDB 中 我知道您可以使用以下命令设置环境变量 set environment username test 但是我需要传递用户名变量特殊字符 所以我需要执行以下操作 set e
  • 如何根据数字/非数字分割字符串(使用正则表达式?)

    我想在 python 中将一个字符串拆分为一个列表 具体取决于数字 而不是数字 例如 5 55 6 5 应该返回 5 55 6 5 我目前有一些代码循环遍历字符串中的字符并使用 re match d 或 D 测试它们 我想知道是否有更好的方
  • 在 TabLayout 中选择自定义选项卡文本颜色

    我正在尝试创建自定义选项卡布局 因为我需要在旁边设置徽章计数器TextView 我已将 id 设置为 android id text1正如文档中提到的 当选择我的自定义选项卡时 TextView 颜色不会自动更改 如何以正确且干净的方式实现