如何更改Selected Tab的颜色

时间:2013-09-20 05:14:15

标签: android android-actionbar

如何在选中标签时更改标签的颜色,请参见下面的屏幕截图:

我在ActionBar中显示橙色,同样地,我想用橙色代替淡蓝色。

要在ActionBar背景中显示橙色,我使用下面的代码:

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

    <style name="Theme.MyAppTheme" parent="android:style/Theme.Holo.Light">
         <item name="android:actionBarStyle">@style/Theme.MyAppTheme.ActionBar</item>
    </style>

   <style name="Theme.MyAppTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#FF4444</item>
    </style>

</resources>

11 个答案:

答案 0 :(得分:16)

我真的建议您使用Actionbar Style Generator

使用该工具,您可以轻松地在工具栏中设置图形元素的主题。

答案 1 :(得分:8)

只需在标签布局中添加以下2个属性即可。

app:tabSelectedTextColor="@color/color_primary_text"
app:tabTextColor="@color/color_secondary_text"

xml中的整个tablayout如下所示

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:background="@color/button_background"
android:fillViewport="true"
app:tabBackground="@drawable/fixed_bottom_button"
app:tabIndicatorColor="@color/color_primary_text"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/color_primary_text"
app:tabTextColor="@color/color_secondary_text" />

答案 2 :(得分:6)

将此函数调用并调用您的Activity并将tabhost作为参数传递

public static void setTabColor(TabHost tabhost) {

            for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
                tabhost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.header_blank); // unselected
            }
            tabhost.getTabWidget().setCurrentTab(0);
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_selected_new); // selected
                                                                            // //have
                                                                            // to
                                                                            // change
        }

按以下方式调用

        setTabColor(tabHost);
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String arg0) {

                setTabColor(tabHost);
            }
             });

希望这对你有用

答案 3 :(得分:4)

如下所示设置您的自定义主题。

<item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item>

之后

<style name="Widget.TabWidget">
    <item name="android:textAppearance">@style/TextAppearance.Widget.TabWidget</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:singleLine">true</item>
</style>  


<style name="TextAppearance.Widget.TabWidget">
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">@android:color/tab_indicator_text</item>
</style>   

或者您可以使用以下代码。

 tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);

 tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#4E4E9C"));

希望它对你有所帮助。

答案 4 :(得分:3)

myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
  @Override
  public void onTabChanged(String tabId) {
    int tab = myTabHost.getCurrentTab();
    View view = myTabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.CYAN);
  }
});

答案 5 :(得分:1)

更改标签栏背景:

actionBar.setStackedBackgroundDrawable(new ColorDrawable(yourOwnColor)); 

答案 6 :(得分:1)

创建一个选择器文件,其中包含您希望的颜色应用于选项卡xml,如下所示:

 <?xml version="1.0" encoding="utf-8"?>
        <!-- 
        Copyright (c) Josh Clemm 2010
         -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <!--  Active tab -->
        <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/dm_tab_highlight" />
        <!--  Inactive tab -->
    <!--    <item android:state_selected="false" android:state_focused="false" -->
    <!-- android:state_pressed="false" android:drawable="@drawable/tabbarbg" /> -->

        <!--  Pressed tab -->
        <item android:state_pressed="true" android:drawable="@drawable/dm_tab_highlight" />


    </selector> 

答案 7 :(得分:0)

你可以像这样使用

tab_background_select.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/tab_background" />// for selected
     <item android:drawable="@drawable/tab" /> // for normal
</selector>

答案 8 :(得分:0)

您可以使用此代码并设置item_tab xml文件的背景

tab_selection.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false" 
      android:drawable="@drawable/tab_bg_selected" />
<item android:state_selected="false" android:state_pressed="false" 
      android:drawable="@drawable/tab_bg_unselected" />
<item android:state_pressed="true" 
      android:drawable="@drawable/tab_bg_pressed" />
</selector>

答案 9 :(得分:0)

您可以使用此代码并设置图标alpha,它看起来像是一个被选中而其他被禁用。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(255);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(128);
                break;
            case 1:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(255);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(128);
                break;
            case 2:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(255);
                tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(128);
                break;
            case 3:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(128);
             tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(255);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

答案 10 :(得分:0)

使用此代码来更改所选标签的颜色:-

tabLayout.setTabTextColors(Color.parseColor("color_for_unselected_tab"), Color.parseColor("color_for_tab"));
for tab-indicator
 tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#627179")));