按下currentTab时如何更改标签的文本颜色?

问题描述

| 在我的seTabColor()中,将标题文本的颜色设置为Gray。我想在按下时将其更改为白色。我该怎么做?
public void setTabColor(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);
} 我想做这样的事情:
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())...
但是我不确定如何使用它来有条件地更改文本颜色。     

解决方法

        首先,请考虑使用XML定义UI。 看一下State List Drawable Resource。您可以定义按下视图,突出显示等时要使用的图像。定义后,便可以像使用其他资源一样使用XML文件。 例:     XML文件保存在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\" />
    ,        试试这个答案,它特别显示:
    <item name=\"android:textColor\">@android:color/tab_indicator_text</item>
您可以通过创建自己的颜色选择器(在项目中创建
res/color/
目录,并在其中创建一个名为file6ѭ的新文件),然后更改上述值以匹配您自己的颜色选择器(
@color/tab_indicator_text
),来覆盖默认值
textColor
tab_indicator_text.xml
文件的内容将是一个选择器列表,类似于此答案中提到的列表:
<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>
    ,        http://developer.android.com/resources/tutorials/views/hello-tabwidget.html只要走到那里,您就会知道您想做什么。 tabHost.getTabWidget()。getChildAt(0).setBackgroundColor(Color.RED);