简体   繁体   中英

How can I set different color for my tab label independent of background color?

How can I set Separate color for my tab label? If I change the background color whole color changes.

my code

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

 <FrameLayout
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">

 </FrameLayout>
 </RelativeLayout>
</TabHost>

在此输入图像描述

You can do such a thing by following this

String TAG_AddData="TAB LABEL";
        /*add Tab in Tabgroup*/
        TabHost host = getTabHost();
                host.addTab(host
                        .newTabSpec(TAG_AddData)
                        .setIndicator(TAG_AddData,
                                getResources().getDrawable(R.drawable.tab_add))
                        .setContent(
                                new Intent(this, AddData_ActivityGroups.class)
                                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

/* Now you can loop through all the tabs ang chnage its label properties*/

    for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {

    TextView tv = (TextView) host.getTabWidget().getChildAt(i)
                        .findViewById(android.R.id.title);
    tv.setTextColor(Color.parseColor("#ffffff"));
    }

    TextView tv = (TextView) host.getCurrentTabView().findViewById(
    android.R.id.title); // for Selected Tab
    tv.setTextColor(Color.parseColor("#000000"));

And same thing you can apply in onTabChanged Method of tabGroup

res/drawable/tabselector.xml

<selector
android:id="@+id/myselector"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
    android:state_focused="false"
    android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/darklogo" />
<item
    android:state_focused="false"
    android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/lightlogo" />

<!-- Focused states -->
<item
    android:state_focused="true"
    android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/lightlogo" />
<item
    android:state_focused="true"
    android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/lightlogo" />

<!-- Pressed -->
<item
    android:state_pressed="true"
    android:drawable="@drawable/lightlogo" />
</selector>

The XML you've included here is a way of defining a drawable that lets you embed a case statement. It presents a different drawable depending on the state of the View it's being assigned to. As a drawable, you should save it as an xml file within the res/drawable folder of your project (for example tabselector.xml ).

To use it for the Tabhost, you need to construct the TabActivity as you normally would (as shown in this tutorial example ).

Then when you add each tab to the host, you specify the tabselector drawable as the indicator as shown for "TAB 1" below.

Drawable mySelector = getResources().getDrawable(R.drawable.tabselector);

mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1", mySelector).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));

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