How to change color of Selected Tab

2019-01-11 17:52发布

How to change color of tab when its selected, see below screen shot:

i am showing Orange color in ActionBar, in a same way i wanna show orange color in place of light blue.

To show Orange color in ActionBar background, i am using below code:

<?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条回答
戒情不戒烟
2楼-- · 2019-01-11 18:43

I really recommend you to use the Actionbar Style Generator.

With that tool you can easily theme your graphic elements in your toolbar.

查看更多
聊天终结者
3楼-- · 2019-01-11 18:43
myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
  @Override
  public void onTabChanged(String tabId) {
    int tab = myTabHost.getCurrentTab();
    View view = myTabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.CYAN);
  }
});
查看更多
我命由我不由天
4楼-- · 2019-01-11 18:43

Create an selector file which consist your desire color apply on tab xml like below:

 <?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> 
查看更多
Evening l夕情丶
5楼-- · 2019-01-11 18:44

Just add the following 2 attributes in your tab layout.

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

Whole tablayout in xml looks like below

<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" />
查看更多
Melony?
6楼-- · 2019-01-11 18:54

put this function and call it to yout Activity and pass tabhost as a parameter

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
        }

call this as following way

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

            @Override
            public void onTabChanged(String arg0) {

                setTabColor(tabHost);
            }
             });

hope this is useful to you

查看更多
登录 后发表回答