I change the color with this code, but the color changes only when i quit from the application and run it again. How can I resolve to change immediately the color?
我使用此代码更改颜色,但只有当我退出应用程序并再次运行时颜色才会更改。如何解决立即更改颜色?
adapter2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view
.findViewById(android.R.id.text1);
text.setTextColor(Color.parseColor(color));
return view;
}
};
2 个解决方案
#1
4
Create res/color/button_text.xml
创建res / color / button_text.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="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML
will apply the color list to a View
:
此布局XML将颜色列表应用于视图:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/button_text" />
And if you want to apply State Color
Dynamically then
如果你想动态应用状态颜色那么
ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed}, //1
new int[]{-android.R.attr.state_focused}, //2
new int[]{android.R.attr.state_selected, android.R.attr.state_pressed} //3
},
new int[] {
Color.RED, //1
Color.WHITE, //2
Color.BLUE //3
}
);
title_txt.setTextColor(myColorStateList);
For more information go to this: http://developer.android.com/guide/topics/resources/color-list-resource.html
有关更多信息,请访问:http://developer.android.com/guide/topics/resources/color-list-resource.html
#2
4
Put this file in res/color folder as textselector_list.xml
将此文件作为textselector_list.xml放在res / color文件夹中
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" android:color="#FFFFFFFF"/>
<!-- pressed -->
<item android:state_focused="true" android:color="#FFFFFFFF"/>
<!-- focused -->
<item android:state_selected="true" android:color="#FFFFFFFF"/>
<!-- selected -->
<item android:color="#FF888888"/>
<!-- default -->
And set this as android:textColor="@color/textselector_list"
to the listView
item i.e. your TextView
or Button
.
并将其设置为android:textColor =“@ color / textselector_list”到listView项,即TextView或Button。
#1
4
Create res/color/button_text.xml
创建res / color / button_text.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="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML
will apply the color list to a View
:
此布局XML将颜色列表应用于视图:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/button_text" />
And if you want to apply State Color
Dynamically then
如果你想动态应用状态颜色那么
ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed}, //1
new int[]{-android.R.attr.state_focused}, //2
new int[]{android.R.attr.state_selected, android.R.attr.state_pressed} //3
},
new int[] {
Color.RED, //1
Color.WHITE, //2
Color.BLUE //3
}
);
title_txt.setTextColor(myColorStateList);
For more information go to this: http://developer.android.com/guide/topics/resources/color-list-resource.html
有关更多信息,请访问:http://developer.android.com/guide/topics/resources/color-list-resource.html
#2
4
Put this file in res/color folder as textselector_list.xml
将此文件作为textselector_list.xml放在res / color文件夹中
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" android:color="#FFFFFFFF"/>
<!-- pressed -->
<item android:state_focused="true" android:color="#FFFFFFFF"/>
<!-- focused -->
<item android:state_selected="true" android:color="#FFFFFFFF"/>
<!-- selected -->
<item android:color="#FF888888"/>
<!-- default -->
And set this as android:textColor="@color/textselector_list"
to the listView
item i.e. your TextView
or Button
.
并将其设置为android:textColor =“@ color / textselector_list”到listView项,即TextView或Button。