【问题标题】:Set textcolor only one tab on tablayout在tablayout中仅设置一个选项卡的文本颜色
【发布时间】:2020-03-31 00:38:53
【问题描述】:

我想在标签布局上设置textColor只有一个标签。默认情况下,一个选项卡必须具有另一种颜色。 这是我在 XML 中的 Tablayout

 <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextColor="@color/colorWhite"
        app:tabIndicatorColor="@color/colorPrimary"
        android:background="@color/colorBackground"
        app:tabMode="fixed" />

它使所有选项卡颜色为白色。 我试过这个:

tabs_main.getTabAt(3)?.icon?.alpha = 225

但它不起作用 如何更改一种标签颜色?

【问题讨论】:

    标签: android kotlin android-tablayout


    【解决方案1】:

    使用 alpha,您更改的是内部文本的不透明度,而不是颜色。

    如果这是您想要实现的目标,请检查 tabs_main.getTabAt(3) 是否不返回 null

    否则,我建议你在标签元素中使用自定义TextView

    例子:

    创建布局:custom_text_view.xml

      <?xml version="1.0" encoding="utf-8"?>
      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/customTabTextView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"
          android:textColor="@color/your_color"
          android:textSize="14sp" />
    

    将布局添加为每个选项卡项的自定义视图,并为每个选项卡项选择所需的textColor

        (0..tabLayout.tabCount).forEach { position ->
            val customTextView = LayoutInflater.from(this).inflate(R.layout.custom_text_view, null)
            // Set the text color
            customTextView.setTextColor(ContextCompat.getColor(applicationContext, R.color.<name_of_color>))
            tabLayout.getTabAt(position)?.customView = customTextView
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多