简体   繁体   English

更改标签指示符背景(适用于所有标签布局视图)

[英]Changing tab indicator background (for all the tablayout view)

I have a question which I can't find an answer to and really have to solve. 我有一个问题,我找不到答案,而实际上必须解决。 I am trying to change the background for the tab layout indicator but JUST FOR IT, ie if the entire tablayout is green and the tablayoutindicator height is 4dp I want the ENTIRE bottom 4dp of the tablayoutview will be in another color (lets say red) is that possible? 我正在尝试更改选项卡布局指示器的背景,但仅此而已,即,如果整个选项卡布局为绿色,并且选项卡布局指示符的高度为4dp,我希望选项卡布局视图的整个底部4dp将采用另一种颜色(让我们说红色)是那可能吗? Before you answer please note I'm not talking about app:tabIndicatorColor property but on the entire height of the tabIndicator (regardless of the tabIndicator itself). 在回答之前,请注意,我不是在谈论app:tabIndicatorColor属性,而是在tabIndicator的整个高度上(无论tabIndicator本身)。

For example, on the attached image the app:tabIndicatorColor is white but the background of it is black while the entire view is a gardient of blue. 例如,在附加的图像上,app:tabIndicatorColor是白色的,但是背景是黑色的,而整个视图都是蓝色的。

在此处输入图片说明

Edit - I managed to do it using a FrameLayout having the bottom view in black while the tablayout is above it with its properties, I'm still looking for a more "clean" way to achieve the same thing 编辑-我设法使用具有黑色底视图的FrameLayout来实现它,而tablayout在其上方具有其属性,我仍在寻找一种更“干净”的方法来实现相同的目的

To achieve what you are looking for you can try the following 要实现您的期望,可以尝试以下方法

Create a selector tab_layout_selector 创建一个选择器tab_layout_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="false" android:state_pressed="false">

    <layer-list>

        <!-- Stripe bottom indicator color -->
        <item android:top="-5dp" android:left="-5dp" android:right="-5dp">
            <shape android:shape="rectangle">
                <stroke android:color="@color/red" android:width="4dp"/>
            </shape>
        </item>

    </layer-list>

</item>

</selector>

Finally, in your TabLayout add 最后,在您的TabLayout中添加

 <android.support.design.widget.TabLayout


    <!-- Whole tab background -->
    android:background="@color/white"

    <!-- Line color -->
    android:tabBackground="@drawable/tab_layout_selector"

    .... Others....
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM