按下时如何更改标签的文字颜色(currentTab)?

时间:2011-06-15 02:36:08

标签: android

在我的seTabColor()中,我将标题文本的颜色设置为灰色。我想在按下时将其更改为白色。我该怎么办?

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())...

但是我不知道如何使用它有条件地改变文本颜色。

3 个答案:

答案 0 :(得分:5)

尝试this answer,特别是显示:

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

您可以通过创建自己的颜色选择器来覆盖默认值textColor(在项目中创建res/color/目录,并在其中创建一个名为tab_indicator_text.xml的新文件),然后更改上面的值与您自己的颜色选择器(@color/tab_indicator_text)匹配。 tab_indicator_text.xml文件的内容将是一个选择器列表,就像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>

答案 1 :(得分:0)

首先,如果可能的话,请考虑用XML定义您的UI。

看看State List Drawable Resource。您可以定义在按下,突出显示等视图时要使用的图像。定义之后,您可以像使用任何其他资源一样使用XML文件。

实施例:     保存在res / drawable / button.xml的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" />

答案 2 :(得分:0)

http://developer.android.com/resources/tutorials/views/hello-tabwidget.html只要去那里,你就会明白你想做什么。

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

相关问题