Android将Drawable和Shape组合成一个Drawable(以编程方式) [英] Android combine Drawable and Shape into a single Drawable (programatically)

查看:69
本文介绍了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>

这篇关于Android将Drawable和Shape组合成一个Drawable(以编程方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆