Android 在可绘制选择器中更改实体的颜色
我有以下可提取文件:Android 在可绘制选择器中更改实体的颜色,android,android-drawable,android-selector,Android,Android Drawable,Android 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
<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文件吗?
我知道可以在运行时更改背景颜色,但我看不到如何获得特定状态的颜色
你可以通过编程来做同样的事情,在这里你可以改变任何东西
通过生成函数并在参数中传递颜色,在运行时显示颜色
希望它能帮助你 您可以为形状项定义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>
您可以在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
}