【发布时间】:2020-03-07 22:21:54
【问题描述】:
我在kotlin中写了一个自定义textview,它用作选择按钮。 意思是,在未选中的状态下,它有一个带角的灰色背景,而在选中的状态下,它的背景会改变颜色。
(按钮中间有文字)
问题: 我需要显示一整组这些按钮,并且对于每个按钮,选定状态的颜色可以随按钮的不同而变化。
所以如果我有:Button1、Button2、Button3,对于它们中的每一个,当它们被选中时,背景颜色是不同的。
我能以某种方式控制可绘制 xml 使用的颜色吗?
按钮背景:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="@color/dynamicColor" /> --> how can I control this color?
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
【问题讨论】:
-
你可以创建一个StateListDrawable,它有状态