使用选择改变TextView的文字颜色 [英] Using selector to change TextView text color

查看:273
本文介绍了使用选择改变TextView的文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个TextView来定义一个tabhost一个TabWidget的风格。

我刚刚创建了一个选择器BGCOLOR和工作正常,但我希望做一个选择的文字颜色,但文字颜色不改变:

这是我tab_text_selector.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
     <项目安卓state_selected =真正的机器人:颜色=@机器人:彩色/白/>
    <项目安卓state_focused =真正的机器人:颜色=@机器人:彩色/白/>
    <项目的android:STATE_ pressed =真正的机器人:颜色=@机器人:彩色/白/>

< /选择器>
 

这是当我试图在一个TextView使用code:

  TextView的txtTab =新的TextView(本);
        txtTab.setTextColor(R.drawable.tab_text_selector);
        txtTab.setBackgroundResource(R.drawable.tab_bg_selector);
        txtTab.setGravity(Gravity.CENTER);
        txtTab.setText(Agregar理念);
 

我知道,文本颜色必须是白色的,在任何情况下,但事实并非如此。

解决方案

1)使用tab_text_selector.xml如下,并把它变成RES /彩色文件夹:

 

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_selected =真正的机器人:颜色=@机器人:彩色/白/>
    <项目安卓state_focused =真正的机器人:颜色=@机器人:彩色/白/>
    <项目的android:STATE_ pressed =真正的机器人:颜色=@机器人:彩色/白/>
    <项目的android:颜色=#504f4f/> <  - 默认的情况下,
< /选择器>
 

和它设置为您的TextView如下..

  TextView的电视=(TextView中)findViewById(R.id.TextView1);
tv.setTextColor(context.getResources()的getColor(R.color.tab_text_selector));
 

2)第二个选项是如果您在使用XML TextView的,而不是使用编程方式,然后使用tab_text_selector.xml如下:

 <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=center_horizo​​ntal
            机器人:文本=TextView的
            机器人:文字颜色=@色/ color_tab_text/>
 

I'm trying to use a TextView to define the style of a TabWidget on a tabhost.

I just created a selector for bgcolor and works fine, but i want to make a selector for textColor but the text color don't change:

This is my tab_text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />

</selector>

And this is the code when i'm trying to use on a textView:

TextView txtTab=new TextView(this);
        txtTab.setTextColor(R.drawable.tab_text_selector);
        txtTab.setBackgroundResource(R.drawable.tab_bg_selector);
        txtTab.setGravity(Gravity.CENTER);
        txtTab.setText("Agregar Idea");

I know the text color must be white in any case but it doesn't.

解决方案

1) Use tab_text_selector.xml as below and put it into res/color folder:

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

And set it to your textview as below..

TextView tv = (TextView) findViewById(R.id.TextView1) ;
tv.setTextColor(context.getResources().getColor(R.color.tab_text_selector));

2) The Second option is If you are using textview in xml rather than using programatically then use tab_text_selector.xml as below :

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="TextView"
            android:textColor="@color/color_tab_text" />

这篇关于使用选择改变TextView的文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆