Manage tab color or state in TabActivity?

Well, this is driving me crazy - I was looking for all the links and examples I can find, and it still seems to me that you have something really obvious. These are the tabs for the 7-day TV guide (usually not with a red arrow, obviously :)) ...

enter image description here

I need to know what is the object (View or Drawable that I assume) that makes up the main element / background of the Tab itself? (as indicated by the red arrow) and how can I access it or automatically change the status color to a list of my choice? Also, how can I get the color status of the TextView indicator to follow suit?

Example. In the above capture, it reads because I set textColor to static gray (instead of the bright white that disappeared on the selected tab). But I want it to automatically become black text on a white tab (selected) and bright white text on black (for unselected).

All help is gratefully received.

+5
source share
2 answers

The view that represents each tab can be changed using

setIndicator(View)

I use this code to create each tab:

View view = buildTabView(this, "Friday");
TabHost.TabSpec spec = tabHost.newTabSpec("cat1").setIndicator(view).setContent(intent);
tabHost.addTab(spec);

public static LinearLayout buildTabView(Context context, String label){
    LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
    final LinearLayout ll = (LinearLayout)li.inflate(R.layout.tab, null);

    // the following lines will change the tabs size depending on the label (text) length.
    // the longer tab text - the wider tabs
    LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, label.length() + 1);
    ll.setLayoutParams(layoutParams);

    final TextView tv = (TextView)ll.findViewById(R.id.tab_tv);         
    tv.setOnTouchListener(new OnTouchListener() {               
        public boolean onTouch(View v, MotionEvent event) {
            ll.onTouchEvent(event);
            return false;
        }
    });

    tv.setText(label);          
    return ll;
}

And here comes layout / tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/tab_bg_selector"
  android:clickable="true"
  >

  <TextView
  android:id="@+id/tab_tv"
  android:layout_width="wrap_content"
  android:layout_height="33dip"
  android:text="Text 1"
  android:textStyle="bold"
  android:textSize="16dip"
  android:gravity="center"
  android:textColor="@drawable/tab_color_selector"
  android:layout_weight="1.0"
  android:clickable="true"
  />

</LinearLayout>

, LinearLayout ( , :)), TextView textColor ( / ..). , , , - :)

+6

, . xml ? xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_tab_about_grey"
          android:state_selected="false" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_tab_about_color" android:state_selected="true"/>
</selector>

, XML .

/ :

intent = new Intent().setClass(this, sms.class);
spec = tabHost.newTabSpec("sms").setIndicator("SMS",
       res.getDrawable(R.drawable.ic_tab_sms))
       .setContent(intent);
tabHost.addTab(spec);

XML - .

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</TabHost>

xml , .

+2

All Articles