【问题标题】:create following code xml programmatically into layerlist android以编程方式将以下代码 xml 创建到 layerlist android
【发布时间】:2015-12-18 09:49:37
【问题描述】:

我必须为许多不同颜色的视图设置背景。但是如果我这样做,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>

【问题讨论】:

  • 你想换什么颜色?
  • @NigamPatro,
  • @NigamPatro,基本上我必须将此 xml 设置为背景,以便可以使用不同颜色代码的视图
  • 意思是,底部?

标签: android android-drawable layer-list android-shape android-shapedrawable


【解决方案1】:

请找到以下代码,

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

参考这个,如有问题请告诉我。

【讨论】:

  • 你使用过的好友, 属性
  • insetLayer2 正在执行此任务。
  • 能不能把上面的内容详细解释一下,好让我明白
  • 你看不懂哪一部分?请通过 cmets。
  • new LayerDrawable(new Drawable[]{layer1, insetLayer2});
猜你喜欢
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
相关资源
最近更新 更多