[英]How to change background color of a tab when clicked on
I have a TabLayout
and it displays about 4 tabs. 我有一个TabLayout
,它显示大约4个选项卡。 When I click on one tab, the background color is a disgusting yellowish highlight (and then goes back to normal when I release). 当我单击一个选项卡时,背景颜色是令人讨厌的淡黄色突出显示(然后在释放时恢复为正常)。 How would I change it so that it is white during the transition to the desired fragment. 在过渡到所需片段的过程中,我将如何更改它使其为白色。
Here is my app theme 这是我的应用主题
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
and here is the Tablayout
and ViewPager
这是Tablayout
和ViewPager
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/headBlue"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/white"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
and here are the dependencies 这是依赖项
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
Create drawable file for selector named tab_selector.xml in your drawable directory. 在可绘制目录中为名为tab_selector.xml的选择器创建可绘制文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_background_selected" android:state_selected="true" />
<item android:drawable="@drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>
Create tab_background_selected.xml file in drawable directory. 在可绘制目录中创建tab_background_selected.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FF0000" />
</shape>
Create tab_background_unselected.xml file in drawable directory. 在可绘制目录中创建tab_background_unselected.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00FF00" />
</shape>
Finally, apply this to your style. 最后,将其应用于您的样式。
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">@drawable/tab_selector</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.