我正在使用数据绑定来更改选项卡布局的选定选项卡的颜色。
@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,(我无法在表布局中找到一个带有表背景属性的设置器)。
发布于 2021-04-13 02:08:34
在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" />https://stackoverflow.com/questions/67067082
复制相似问题