RN5*_*N55 8 android android-layout
我想访问定义为可绘制资源的颜色资源,并希望在JAVA中切换背景颜色,基本上使用下面提到的可绘制XML来改变按钮的背景.我尝试访问按钮并修改颜色属性,但这会将按钮的形状更改为正常的方形.我想保持可绘制XML中定义的形状并手动更改背景颜色.
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#EAEAEA" />
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="1dip"
android:topLeftRadius="1dip"
android:topRightRadius="8dip" />
</shape>
</item>
<item><shape android:shape="rectangle">
<solid android:color="#EAEA00" />
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="1dip"
android:topLeftRadius="1dip"
android:topRightRadius="8dip" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
你有两种可能性:
myButton.setBackgroundColor(Color.CHOOSE_ONE);myButton.setBackgroundResource(R.color.youCustomColor);如果要从十六进制值设置颜色,只需使用类的静态方法Color:
myButton.setBackgroundColor(Color.parseColor("#RRGGBB"));
//http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4161 次 |
| 最近记录: |