Android将Drawable和Shape组合成一个Drawable(以编程方式)
问题描述:
我正在以编程方式设置RadioButton的 android:drawable
:
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.
未检查的行为在第二行addState中设置,并且可以正常工作.我的问题是如何以编程方式绘制圆,然后以某种方式将其与我从资源中读取的可绘制对象相结合.
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.
答
这是一个层列表.
<?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>