开发者

how to change text color of tabs when pressed (currentTab)?

In my seTabColor() I'm setting the color of the title text to Gray. I want to change it to white when pressed. How can I do it?

public void s开发者_如何学运维etTabColor(TabHost tabHost) {
        for(int i = 0; i<tabHost.getTabWidget().getChildCount(); i++) {
//          tabHost.getTabWidget().getChildAt(i).setBackgroundResource(r[i]);
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK);
            TextView t = (TextView) getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            t.setTextSize(9 * getResources().getDisplayMetrics().density);
//          tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 58;
//          tabHost.getTabWidget().getChildAt(i).().height = 58;
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.GRAY);

}

I wanna do something like: tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())...

But im not sure how to use that to conditionally change the text color.


Try this answer, which in particular shows:

    <item name="android:textColor">@android:color/tab_indicator_text</item>

You can override that default textColor by creating your own color selector (create the res/color/ directory in your project, and create a new file in there, called tab_indicator_text.xml), and changing the value above to match your own color selector (@color/tab_indicator_text). The contents of the tab_indicator_text.xml file will be a selector list, like the one mentioned in this answer:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/white" />
    <item android:state_focused="true" android:color="@color/white" />
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:color="#bfbfbf" />
</selector>


First, look into defining your UI in XML if that's possible.

Take a look at State List Drawable Resource. You can define what image to use when a view is pressed, highlighted, etc. After defined, you can then use your XML file like any other resource.

Example: XML file saved at res/drawable/button.xml:

<?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/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

This layout XML applies the state list drawable to a Button:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />


http://developer.android.com/resources/tutorials/views/hello-tabwidget.html just go there you waill come to know what u want to do.

tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜