[英]Change colors of solid in drawable selector
我有以下可绘制对象:
<item android:state_pressed="true" android:text="texting .....">
<shape
xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:width="@dimen/circleStrokeThickness" android:color="@color/earlGreen" />
<solid
android:color="@color/lightGrey"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
</item>
<item android:state_pressed="false" android:text="texting .....">
<shape
xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:width="4dp" android:color="@color/earlGreen" />
<solid
android:color="@android:color/white"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
</item>
但是,我需要一些类似的颜色,其中纯色不同,但其余颜色相同。 有什么简单的方法可以做到这一点,还是应该只定义一些xml文件? 我知道可以在运行时更改背景颜色,但是我看不到如何获得特定状态的颜色。
您可以为形状项目<item android:id="@+id/shape_bacground"../>
定义ID,然后在运行时必须获取视图背景并将其转换为LayerDrawable
并使用findDrawableByLayerId()
查找形状并使用setColor()
设置颜色。 这是示例代码:
可绘制的xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/float_button_shadow1">
</item>
<item
android:id="@+id/shape_bacground"
android:bottom="2dp"
android:left="1dp"
android:top="1dp"
android:right="1dp">
<shape android:shape="oval" >
<solid android:color="#1E88E5" />
</shape>
</item>
</layer-list>
变色
try {
LayerDrawable layer = (LayerDrawable) view.getBackground();
GradientDrawable shape = (GradientDrawable) layer
.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);// set new background color here
rippleColor = makePressColor();
} catch (Exception ex) {
// Without bacground
}
您可以通过编程来执行相同的操作,在其中可以通过运行函数并在参数中传递Color来在运行时更改任何颜色。
ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(CommonUtilities.color);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(CommonUtilities.stroke);
sd1.setPadding(15, 10, 15, 10);
sd1.getPaint().setPathEffect(
new CornerPathEffect(CommonUtilities.corner));
ln_back.setBackgroundDrawable(sd1);
希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.