2017-07-02 7 views
0

私はプログラム的に私のicon.xmlのような図形を描画したいのですが、私は最初の円に二円を埋め込むことはできませんが、これは私のコードです:レイヤーリストドロワブルをプログラムで作成する方法は?

icon.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/ok"> 
<shape 
    android:shape="oval"> 
    <solid android:color="#ffffff" /> 
    <size android:width="33px" android:height="33px"/> 
    <stroke android:width="4px" 
     android:color="#ff0000"/> 
    <padding android:bottom="8px" 
     android:left="8px" 
     android:right="8px" 
     android:top="8px"/> 
</shape> 
</item> 
<item> 
<shape android:shape="oval"> 
    <solid android:color="#ccff0000"/> 
</shape> 
</item> 

</layer-list> 

MainActivity.java

間違って何
GradientDrawable layer1 = new GradientDrawable(); 
    layer1.setShape(GradientDrawable.OVAL); 
    layer1.setSize(33,33); 
    layer1.setColor(Color.WHITE); 
    layer1.setStroke(4,Color.RED); 

    GradientDrawable layer2 = new GradientDrawable(); 
    layer2.setShape(GradientDrawable.OVAL); 
    layer2.setColor(Color.BLUE); 

    InsetDrawable insetLayer2 = new InsetDrawable(layer1, 8, 8, 8, 8); 

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] 
    {insetLayer2,layer2}); 
    button.setBackground(layerDrawable); 

ありがとうございます。

答えて

0

最後に私は答えを得ました。私の問題は、円の外にパディングを設定する方法でしたが、 2つのグラデーションを別々にsetLayerInset(int index, int l, int t, int r, int b)を使用してプログラムでパディングを設定することができます。

関連する問題