4

我有这个xml;

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:drawable="@drawable/back" />
    <item>
        <shape>
            <solid/>
            <stroke android:width="1dip" android:color="#25786" />
            <corners android:radius="10dip"/>
            <padding android:left="0dip" android:top="0dip" 
                     android:right="0dip" android:bottom="0dip" />
        </shape>
    </item> 
</layer-list> 

现在我通过代码来做:

Drawable[] layers = new Drawable[2];

    ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
    sd1.getPaint().setColor(0xFFFFFFFF);
    sd1.getPaint().setStyle(Style.STROKE);
    sd1.getPaint().setStrokeWidth(1);

    layers[0] = sd1;

    LayerDrawable composite = new LayerDrawable(layers);

但我无法对其进行<corners android:radius="10dip"/>编码<item android:drawable="@drawable/back" />

怎么做?

编辑 1

Drawable[] layers = new Drawable[2];

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(0xFFFFFFFF);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(1);
sd1.getPaint().setPathEffect(new CornerPathEffect(10));

layers[1] = sd1;
layers[0] = getResources().getDrawable(R.drawable.pie_chart_back);

LayerDrawable composite = new LayerDrawable(layers);
4

2 回答 2

5

就像在您的 XML 中一样,您需要三个Drawable,而不是一个。一个是LayerDrawable包含其他的。另一个缺失的可能是BitmapDrawable取决于@drawable/back.

您正在寻找的角落效果在CornerPathEffectDrawable的中Paint,请参阅Paint.setPathEffect

于 2013-04-22T08:53:56.367 回答
0

尝试这个...!

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#e1e1e1" />

    <stroke
        android:width="2dp"
        android:color="#808080" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>
于 2013-07-04T12:59:37.450 回答