在tablayout中将颜色设置为未选中的制表符指示符

Sni*_*per 5 android

我正在使用谷歌提供的tablayout,除了未选择的标签指示器的颜色外,一切正常.我无法设置未选中的标签指示颜色.

 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.design.widget.AppBarLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

     <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         android:background="?attr/colorPrimary"
         app:layout_scrollFlags="scroll|enterAlways"
         app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

     <android.support.design.widget.TabLayout
         android:id="@+id/tabs"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:tabMode="fixed"
         style="@style/MyCustomTabLayout"

         />
 </android.support.design.widget.AppBarLayout>

 <android.support.v4.view.ViewPager
     android:id="@+id/viewpager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

Car*_*Gil 16

尝试这样改变你想要的颜色:

在drawable文件夹中创建此文件

tab_indicator_color.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- UNSELECTED TAB STATE -->
    <item android:state_selected="false" android:state_pressed="false">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- Bottom indicator color for the UNSELECTED tab state -->
            <item android:top="-5dp" android:left="-5dp" android:right="-5dp">
                <shape android:shape="rectangle">
                    <stroke android:color="#65acee" android:width="2dp"/>
                </shape>
            </item>
        </layer-list>
    </item>


</selector>
Run Code Online (Sandbox Code Playgroud)

并在app:tabBackground中设置你的.xml文件之后:

 <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        style="@style/MyCustomTabLayout"
        app:tabBackground="@drawable/tab_indicator_color" />
Run Code Online (Sandbox Code Playgroud)

  • 不起作用,在整个选项卡上创建了边框,我只需要底线 (5认同)

小智 11

ic_tab_indicator_default.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item android:gravity="bottom">
        <shape android:shape="rectangle">
            <size android:height="1dp" />
            <solid android:color="@color/steel_blue" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

ic_tab_indicator_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item android:gravity="bottom">
        <shape android:shape="rectangle">
            <size android:height="3dp" />
            <solid android:color="@color/congress_blue" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

selector_tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_tab_indicator_selected" android:state_selected="true" />
    <item android:drawable="@drawable/ic_tab_indicator_default" />
</selector>
Run Code Online (Sandbox Code Playgroud)

选项卡布局

<com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicator="@android:color/transparent"
        app:tabBackground="@drawable/selector_tab_indicator">
Run Code Online (Sandbox Code Playgroud)


Sun*_*ong 10

**1.app:**

    tabBackground="@drawable/tablayout_selector"

**2.tablayout_selector.xml**

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

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/shl_common_default_color" />
        </shape>
    </item>
    <item>
        <inset android:insetBottom="1.5dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/white" />
            </shape>
        </inset>
    </item>
</layer-list>


or

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

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:gravity="bottom">
        <shape android:shape="rectangle">
            <solid android:color="@color/shl_common_default_color" />
            <size android:height="1.5dp" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)


Muh*_*lib 7

对于使用新 com.google.android.material.tabs.TabLayout 的人来说,如果您应用本文中正确答案给出的解决方案,选项卡的所有四个边都会出现角点

要解决这个问题,请使用以下“hack”

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- UNSELECTED TAB STATE -->
    <item android:state_selected="false" android:state_pressed="false">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- Bottom indicator color for the UNSELECTED tab state -->
            <item  android:top="-10dp" android:left="-1000dp" android:right="-1000dp">
                <shape android:shape="rectangle" >
                    <stroke android:color="#4cffffff" android:width="1dp"/>
                </shape>
            </item>
        </layer-list>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)