【问题标题】:change unselected tab color of tablayout dynamically动态更改选项卡布局的未选择选项卡颜色
【发布时间】:2021-07-08 01:08:46
【问题描述】:

我正在使用数据绑定来更改选项卡布局的选定选项卡的颜色

@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 属性的设置器)

【问题讨论】:

    标签: android xml data-binding android-viewpager android-tablayout


    【解决方案1】:

    在 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" />
    

    【讨论】:

    • 这对动态改变标签的背景颜色没有帮助,app:tabBackground 这仅适用于 textColor
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多