[英]Set textview focus color programmatically / change focus color in themes
1) Is it possible to set a TextView's color programmatically? 1)是否可以以编程方式设置TextView的颜色? If so what's the easiest way?
如果是这样,最简单的方法是什么?
I want something else other that the default 4.0+ light-blue color. 我想要其他其他东西,默认4.0+浅蓝色。
I found and tried the following code without success: 我发现并尝试了以下代码但没有成功:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(0x1A000000));
states.addState(new int[] {android.R.attr.state_focused}, new ColorDrawable(0x1A000000));
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
tv.setBackgroundDrawable(states);
} else {
tv.setBackground(states);
}
I do not wish any XML involved. 我不希望涉及任何XML。
2) Can I change the focus color in my themes in general? 2)我可以更改主题中的焦点颜色吗? If yes how?
如果有,怎么样?
XML is obviously fine here. XML在这里显然很好。
You can use ColorStateList , to specify the state color programmatically. 您可以使用ColorStateList以编程方式指定状态颜色。
int[][] states = new int[][] {
new int[] { android.R.attr.state_pressed}, // pressed
new int[] { android.R.attr.state_focused}, // focused
new int[] { android.R.attr.state_enabled} // enabled
};
int[] colors = new int[] {
Color.parseColor("#008000"), // green
Color.parseColor("#0000FF"), // blue
Color.parseColor("#FF0000") // red
};
ColorStateList list = new ColorStateList(states, colors);
textView.setTextColor(list);
textView.setClickable(true);
textView.setFocusableInTouchMode(true);
使用http://android-holo-colors.com/生成仅使用特定颜色编辑文本的主题。
Create file res/drawable/text_color.xml: 创建文件res / drawable / text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
<item android:color="#000000" />
</selector>
Then use @drawable/text_color
from xml (or R.drawable.text_color
from code) as text color for your TextView. 然后使用xml中的
@drawable/text_color
(或代码中的R.drawable.text_color
)作为TextView的文本颜色。
Do you mean how to set color if some event happents? 你是说如果发生某些事件,如何设置颜色? If it is then you can set textView color as follow:
如果是,那么你可以设置textView颜色如下:
textView.setTextColor("give color-code");
You need to create style for that. 你需要为它创造风格。 By using style you can modify text color, background color, etc whatever you want.
通过使用样式,您可以随意修改文本颜色,背景颜色等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.