简体   繁体   中英

android change selected tab background color

I came from objective-c and I am an Android newbie. I am using following method that intends to change tabColor for index 0. But I would like to change default grey tab when selected. Thank you.

mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.CYAN);

Use setOnTabChangedListener(TabHost.OnTabChangeListener l) on the TabHost:

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

Maybe there is a simpler way, i dont have use it before ;)

Android allows for a StateList drawable xml file that is the intended way to get the effect you are after.

read about it here

The idea is you make an xml file that declares a different drawable (or color if you want plain colors) for each state. Then when you are apply that statelist drawable as the background of your View, and it will handle the "magic" of switching your view image for you so that you don't have to worry about doing it manually from java code.

so your code snippet would look something like this:

myTabHost.getTabWidget().getChildAt(tab).setBackgroundResource(R.drawable.your_state_list_filename);

Here is an example of a state list file that I've used on a button. You can copy this into an xml file in your drawables folder, then modify it to use whichever states and images you want.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
       android:drawable="@drawable/darkblue1" /> <!-- pressed -->
 <item android:state_focused="true"
       android:drawable="@drawable/darkblue1" /> <!-- focused -->
 <item android:drawable="@drawable/lightblue1" /> <!-- default -->
 </selector>

I think (but am not certain) that to use colors instead of drawables you'd just change "@drawable/blahblah" to "#FF121212" where the first two digits are alpha, and the next 6 are hex value for the color you want.

I just changed the markup of the TabHost

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@android:color/transparent">

I have used this one to solve my problem:

    tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabs.setSelectedIndicatorColors(Color.RED);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM