如何解决动态更改 tablayout 的未选中标签颜色
我正在使用数据绑定来更改选项卡布局的所选选项卡的颜色
@BindingAdapter(value = ["tabIndicatorColor","context"])
fun setSelectedTabIndicatorColor(tabLayout: TabLayout,color: Int,context: Context) {
tabLayout.setSelectedTabIndicatorColor(getColor(context,color))
}
并从 tabLayout 视图设置它
<variable
name="professionalTypeColor"
type="Integer" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_images"
android:layout_width="0dp"
android:layout_height="5dp"
tabIndicatorColor="@{professionalTypeColor}"
context="@{context}"
app:tabPaddingEnd="8dp"
app:tabPaddingStart="8dp" />
我在此之前所做的与我想要的完全一样,但是对于未选择的选项卡,我无法为其制作数据绑定适配器,因此我动态更改了它的颜色, 我尝试使用
app:tabBackground="@color/grey"
或
app:tabBackground="@drawable/selector_tab_indicator"
但这需要预定义颜色或具有两种颜色(已选择,未选择)的可绘制对象,这不是我想要的结果, 我的问题是如何制作数据绑定适配器来动态设置 tabBackground,(我在 tablayout 中找不到具有 tabbackground 属性的设置器)
解决方法
在 style.xml 中创建一个样式并调用如下的 xml 布局
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@color/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">@color/colorAccent</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">@dimen/title_text_size</item>
<item name="android:textColor">@color/secondaryText</item>
<item name="textAllCaps">false</item>
<item name="android:textStyle">normal</item>
</style>
在这里打电话
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="@android:color/white"
app:tabMode="scrollable"
**style="@style/MyCustomTabLayout"**
app:tabGravity="fill" />