android:以编程方式创建具有状态的自定义形状

时间:2018-11-26 12:14:11

标签: android kotlin android-drawable programmatically

如何在Kotlin(或Java)中使用状态创建形状 例如我想用Kotlin(或java)创建以下形状

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="999dp" />
        <stroke android:width="6dp" android:color="#bfbfbf" />
        <solid android:color="#f44336" />
    </shape>
</item>

<item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="999dp" />
        <stroke android:width="2dip" android:color="#E0E0E0" />
        <solid android:color="#f44336" />
    </shape>
</item>

<item android:state_checked="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="999dp" />
        <stroke android:width="2dip" android:color="#E0E0E0" />
        <solid android:color="#f44336" />
    </shape>
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="999dp" />
        <stroke android:width="6dp" android:color="#bfbfbf" />
        <solid android:color="#f44336" />
    </shape>
</item>

我看到了link1link2,但我不知道如何创建可绘制状态

0 个答案:

没有答案