How to change color of Selected Tab

后端 未结 10 1734
说谎
说谎 2020-12-03 10:42

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 ligh

相关标签:
10条回答
  • 2020-12-03 11:19

    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

    0 讨论(0)
  • 2020-12-03 11:22

    you can use like this

    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>
    
    0 讨论(0)
  • 2020-12-03 11:25

    use this code in order to change the color of selected tab:-

    tabLayout.setTabTextColors(Color.parseColor("color_for_unselected_tab"), Color.parseColor("color_for_tab"));
    for tab-indicator
     tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#627179")));
    
    0 讨论(0)
  • 2020-12-03 11:26

    you can use this code and set the background of your item_tab xml file

    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>
    
    0 讨论(0)
提交回复
热议问题