更改焦点上可点击 TextView 的颜色并点击?
我有一个可点击的 TextView,我想给它一些颜色。但我不知道怎么办。以下是我正在使用的两个文件中的相关代码片段:
TextView title = new TextView(this);
title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
title.setTextColor(R.color.textcolor);
title.setText(titleLine);
title.setTypeface(null, Typeface.BOLD);
title.setClickable(true);
title.setId(idLine);
title.setFocusable(true);
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Irrelevant code */
}
});
这是我的 textcolor.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000"/> <!-- focused -->
<item android:color="#000000"/> <!-- default -->
</selector>
当我通过键入 title.setTextColor(R.color.textcolor);< 使用 textcolor 文件时/em>,无论我是否按下它,文本颜色都会变成灰色。这很奇怪,因为我在所有颜色字段中都写了“#000000”。
但是,如果我删除 setTextColor 代码,使 textView 变为浅灰色,当我按下它时,它会变成黑色。但这不是我想要的颜色。
那么,有人可以帮我解决这个问题吗?
只是为了澄清:我希望能够指定文本正常、按下和聚焦时的颜色。
I have a clickable TextView that I want to give some colors to. But I don't know how. Here are the relevant code snippets from my two files that I'm working with:
TextView title = new TextView(this);
title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
title.setTextColor(R.color.textcolor);
title.setText(titleLine);
title.setTypeface(null, Typeface.BOLD);
title.setClickable(true);
title.setId(idLine);
title.setFocusable(true);
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Irrelevant code */
}
});
And this is my textcolor.xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000"/> <!-- focused -->
<item android:color="#000000"/> <!-- default -->
</selector>
When I use the textcolor-file by typing title.setTextColor(R.color.textcolor);, the textcolor just becomes grey, regardless if I press it or so. Which is strange since I have written "#000000" in all color fields.
But if I remove the setTextColor code, gets the textView a light grey color, and when I press it, it becomes black. But that aren't the colors that I want.
So, can anyone help me with this problem?
Just to clarify: I want to be able to specify the colors for the text when it's normal, pressed and focused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果要从代码中设置有状态颜色,则需要传入
ColorStateList
作为setTextColor
将 int 传递给该方法会导致将颜色设置为所有状态。看起来你的 xml 也不完全正确。ColorStateList
文档中的示例如下所示(应如下所示:res/color/selector_txt.xml
):关于如何设置
ColorStateList
的 UPD文本颜色:注意:
createFromXml(Resources, XmlPullParser parser)
方法在 API 级别 23 中已弃用。使用
createFromXml(Resources, XmlPullParser parser, Theme)
使用 XML 非常简单:
If you want to set stateful color from code, you need to pass in
ColorStateList
as an argument tosetTextColor
passing an int to the method results in setting the color to all the states. It also looks like your xml is not totally correct. Example fromColorStateList
docs looks like(should be located like this:res/color/selector_txt.xml
):UPD on how to set a
ColorStateList
to text color:Note: The method
createFromXml(Resources, XmlPullParser parser)
was deprecated in API level 23.Use
createFromXml(Resources, XmlPullParser parser, Theme)
With XML its as easy as:
第1步:在xml中设置文本颜色,如下所示
第2步:创建 res/color/text_color.xml
Step 1: Set the text color in xml like this
Step2: Create res/color/text_color.xml
试试这个..它对我有用:
文件名:res/color/bg_tab_text_color.xml
尝试将xml布局中的颜色设置为:
Try this one.. It worked for me:
File name: res/color/bg_tab_text_color.xml
Try setting the color in xml layout as:
查看 R.java 类(它是自动生成的)。你有类似的东西:
所以在你的代码中:
你不是从textcolor.xml设置值,而是从R.java设置int(其中包含textcolor.xml地址)。设置颜色的有效方法是:
Look in R.java class (it's generated automatically). You have something like that:
So in your code in line:
you're not setting values from textcolor.xml but int from R.java (which contains textcolor.xml address). The valid way to set color is:
这是一种非常简单的编程方法:
Here is a very simple way programmatically:
这很简单。试试这个..它对我有用:
文件名:res/color/bg_tab_text_color.xml
尝试将 xml 布局中的颜色设置为:
It's very easy.Try this one.. It worked for me:
File name: res/color/bg_tab_text_color.xml
Try setting the color in xml layout as:
在 res/drawable/tab_textColor.xml 中:
In res/drawable/tab_textColor.xml:
在 res/color/text_selector.xml 中:
在布局中:
在代码中:
In res/color/text_selector.xml:
In layout:
In code:
对我来说,这种方法有效:
For me just this way worked: