0

我必须将它的背景设置为许多不同颜色的视图。但是如果我这样做,xml将是大约20个文件,这是巨大的,所以我想将下面的xmllayer-list转换成程序的方式转换成完全独立的模块,甚至没有一个xml,这样它就可以重用,请帮帮我伙计们。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    //i can create this drawable
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff2632"/>
        </shape> </item>

    //I don't know how to achieve this one, especially this "bottom property"
    <item android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#d7ffa2"/>
        </shape> </item>
</layer-list>
4

1 回答 1

4

请找到以下代码,

// This is the first item in your XML file
GradientDrawable layer1 = new GradientDrawable();
layer1.setShape(GradientDrawable.RECTANGLE);
layer1.setColor(Color.parseColor("#ff2632"));

// This is your second item in your XML file
GradientDrawable layer2 = new GradientDrawable();
layer2.setShape(GradientDrawable.RECTANGLE);
layer2.setColor(Color.parseColor("#d7ffa2"));

// This will give the bottom space which you are unable to do    
InsetDrawable insetLayer2 = new InsetDrawable(layer2, 0, 0, 0, 2);

// This is the final drawable which is to be used
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{layer1, insetLayer2});

参考这个,让我知道问题。

于 2015-12-18T10:24:04.373 回答