Android combine Drawable and Shape into a single Drawable (programatically)

被刻印的时光 ゝ 提交于 2020-12-29 13:24:09

问题


I'm setting android:drawable of a RadioButton programatically like this:

Drawable unchecked = getResources().getDrawable(R.drawable.ic_room_car);

Drawable d= getResources().getDrawable(R.drawable.ic_room_car);

Shape circle = ?????;
??? checked = combine circle and d


SateListDrawable states= new StateListDrawable();
states.addState(new int[] {android.R.attr.state_checked}, checked);
states.addState(new int[] {}, unchecked);

((RadioButton)findViewById(R.id.icon_1)).setButtonDrawable(states);

Basically it has a custom icon that I'm reading from resources when it's unchecked, and when checked I would like to draw a circle behind the icon.

The unchecked behavior is set in the second addState line, and it's working. My problem is how to draw a circle programatically and then somehow combine it with the drawable I'm reading from resources.


回答1:


It's a layer-list.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_launcher" android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp">
    </item>
    <item>
       <shape android:shape="oval">
            <solid android:color="@android:color/transparent"></solid>
            <stroke android:color="#4b14b1" android:width="1dp"></stroke>
            <size android:height="50dp" android:width="50dp"></size>
        </shape>
    </item>
</layer-list>


来源:https://stackoverflow.com/questions/34531007/android-combine-drawable-and-shape-into-a-single-drawable-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!