我想实现点击效果和选定的标签背景,我使用下面的代码来更改选定的标签背景。
myLayout.xml代码
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="?attr/colorPrimary"
app:tabBackground="@drawable/tab_color_selector"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="2dp"
app:tabGravity="fill"
/>
tab_color_selector里面的drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorTabPrimary"
android:state_selected="true"/>
<item android:drawable="@color/colorPrimary"/>
</selector>
上面的代码完美无缺,
要提供点击效果,我必须使用以下
app:tabBackground="?attr/selectableItemBackground"
我如何将两者合并?
答案 0 :(得分:1)
要更改所选标签的textColor
,您只需使用TabLayout
的属性。
app:tabSelectedTextColor="@android:color/white"
更改所选标签的背景,请执行以下操作
app:tabBackground="@drawable/selector_tab"
创建一个xml文件
selector_tab.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<solid android:color="@color/colorGreen" />
</shape>
</item>
<item android:state_selected="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>
答案 1 :(得分:0)
尝试以下
tab_color_selector.xml drawable将ripple.xml添加到state_pressed
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue_selected_tab"
android:state_selected="true"/>
<item android:drawable="@drawable/ripple" android:state_pressed="true"/>
<item android:drawable="@color/colorPrimary"/>
</selector>
ripple.xml drawable
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/blue_selected_tab"
tools:targetApi="lollipop"> <!-- ripple effect color -->
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="@color/blue_selected" /> <!-- background color -->
</shape>
</item>