简体   繁体   English

单击时如何更改选项卡的背景色

[英]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 这是TablayoutViewPager

<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'

Step 1 第1步

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>

Step 2 第2步

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>

Step 3 第三步

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>

Step 4 第四步

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.

相关问题 点击textview时如何改变背景颜色和文字颜色? - How to change the background color and text color when textview is clicked? 单击时如何更改操作栏中图标的背景颜色? - How to change the background color of an icon in the action bar, when clicked? 如何更改一次单击时和再次单击时列表视图的背景色,我需要不使用选择器来更改背景色 - how to change the background color of a listview when clicked once and when clicked again i need to change the background color without using selector Android单击时更改背景颜色 - Android Change background color when clicked 单击时更改 FloatingActiongButton 背景和图像颜色 - Change FloatingActiongButton background and image color when clicked 单击此按钮时更改textview的背景颜色 - Change background color of textview when on clicked on this Android:单击时更改ClickableSpan的背景颜色 - Android: Change the background color of a ClickableSpan when clicked 单击时更改列表视图项的背景颜色 - change the background color of a listview item when clicked 单击时更改按钮的背景色 - Change background color of button when clicked 使用 TabLayout 时如何更改选项卡背景颜色? - How do I change a tab background color when using TabLayout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM