简体   繁体   English

以编程方式将以下代码xml创建到layerlist android中

[英]create following code xml programmatically into layerlist android

I have to set background of this to many views with different colors. 我必须将此背景设置为具有不同颜色的许多视图。 but if I make it so, the xml will be around 20 files, which is huge, so I want to convert the following xml layer-list into progmatical way into fully independent module without even a single xml, so that it can be reusable, please help me guys. 但是,如果我这样做的话,那么xml大约有20个文件,这是巨大的,因此我想将以下xml layer-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>

Please find the below code, 请找到以下代码,

// 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});

Refer this, and let me know for issues. 请参阅此,让我知道有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM