[英]How to change colors in an android app?
我想在色盲人的选项菜单中更改颜色,当时我想拥有两个strings.xml文件,并在按下按钮时在它们之间切换。 更改具有在strings.xml中声明的颜色的ImageButton
颜色的正确方法是什么?
final Switch colorsChange= (Switch) findViewById(R.id.switch_colors);
colorsChange.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Color blind friendly colors here
} else {
// Normal RGB colors here
getResources().getColor(R.color.defaultColors);
}
}
});
我想尽可能在所有活动中进行这些设置
button.setColor(Color.RED); 或者您可以使用button.setColor(code color);
您应该在colors.xml中声明颜色
但是,仍然需要在string.xml中声明<string name="red">#ff0000</string>
ImageButton.setBackgroundColor(Color.parseColor(getString(R.string.red));//if using string.xml
or
ImageButton.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));//if using colors.xml
或者,或者:
ImageButton.setBackgroundColor(Color.RED); // From android.graphics.Color
或者,更多专业技能:
ImageButton.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB
您应该在color.xml中定义颜色(在“值”目录中)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_name">#e6e6e6</color>
</resources>
并引用它:
在xml文件中:
@ color / color_name
在Java类中:
getResources()。getColor(R.color.color_name);
android:onClick="methodName"
对按钮进行操作 getResources().getColor(R.color.idname);
获得颜色getResources().getColor(R.color.idname);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.