将TabLayout背景设置为透明

时间:2018-07-17 21:44:51

标签: android tabs android-selector

我正在尝试更改TabLayout's背景的透明性。我在styles.xml中设置整个应用程序的背景,TabLayout使用该颜色作为背景。奇怪的是,如果我将@android:color/transparent更改为白色或红色,则背景颜色的确发生了变化,但透明的却没有:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

    <android.support.design.widget.TabLayout
        android:id="@+id/main_pager_dots"
        android:layout_gravity="bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:tabBackground="@drawable/tab_selector"
        app:tabGravity="center"
        android:visibility="gone"
        app:tabIndicatorHeight="0dp"/>

</RelativeLayout>

styles.xml

<resources>
    <style name="Main" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">#000000</item>
    </style>
</resources>

tab_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/tab_indicator_selected" android:state_selected="true"/>

    <item android:drawable="@drawable/tab_indicator_default" />
</selector>

tab_indicator_selected.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item>
        <shape android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="4dp"
            android:useLevel="false"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>

tab_indicator_default.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="2dp"
            android:useLevel="false">
            <solid android:color="@android:color/darker_gray" />
        </shape>
        <solid android:color="@android:color/transparent"/>
    </item>
</layer-list>

1 个答案:

答案 0 :(得分:0)

尝试将Alpha添加到背景中 styles.xml

<resources>
    <style name="Main" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">#00000000</item>
    </style>
</resources>