[英]Set textcolor only one tab on tablayout
I want to set textColor
only one tab on tablayout.我想在 tablayout 上只设置
textColor
一个选项卡。 One tab must have another color by default.默认情况下,一个选项卡必须具有另一种颜色。 Here is my Tablayout in XML
这是我的 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" />
It make all tabs color white.它使所有标签颜色为白色。 I have tried this:
我试过这个:
tabs_main.getTabAt(3)?.icon?.alpha = 225
but It doesn't work How can I change one tab color?但它不起作用如何更改一种标签颜色?
With alpha you are changing the opacity of the inside text, not the color.使用alpha,您正在更改内部文本的不透明度,而不是颜色。
If this is what you want to achieve, then check if tabs_main.getTabAt(3)
doesn't return null .如果这是您想要实现的目标,请检查
tabs_main.getTabAt(3)
是否不返回null 。
Otherwise, I suggest you to use a custom TextView
inside the tab element.否则,我建议您在 tab 元素中使用自定义
TextView
。
Example:例子:
Create a layout : custom_text_view.xml创建布局: 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" />
Add the layout as custom view for each tab item, and choose the desired textColor
for each of them.为每个选项卡项添加布局作为自定义视图,并为每个选项卡选择所需的
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.