[英]Set textview focus color programmatically / change focus color in themes
1)是否可以以编程方式设置TextView的颜色? 如果是这样,最简单的方法是什么?
我想要其他其他东西,默认4.0+浅蓝色。
我发现并尝试了以下代码但没有成功:
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);
}
我不希望涉及任何XML。
2)我可以更改主题中的焦点颜色吗? 如果有,怎么样?
XML在这里显然很好。
您可以使用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/生成仅使用特定颜色编辑文本的主题。
创建文件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>
然后使用xml中的@drawable/text_color
(或代码中的R.drawable.text_color
)作为TextView的文本颜色。
你是说如果发生某些事件,如何设置颜色? 如果是,那么你可以设置textView颜色如下:
textView.setTextColor("give color-code");
你需要为它创造风格。 通过使用样式,您可以随意修改文本颜色,背景颜色等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.