简体   繁体   English

Android LayerDrawable:如何以编程方式设置图层坐标?

[英]Android LayerDrawable: How to set layer coordinates programmatically?

The following XML code of a layer-list ist given: 给定的以下层列表的XML代码:

<layer-list>
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/my_color_a" />
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/my_color_b" />
        </shape>
    </item>
</layer-list>

To do some things dynamically I need to rebuild this code programmatically as a LayerDrawable class. 为了动态地执行某些操作,我需要以编程方式将其作为LayerDrawable类重建。 This code exists: 此代码存在:

ShapeDrawable layer0 = new ShapeDrawable(new RectShape());
ShapeDrawable layer1 = new ShapeDrawable(new RectShape());
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {layer0, layer1});

How do you translate android:left="5dp" to Java code? 您如何将android:left="5dp"为Java代码? There is no setter for this property. 此属性没有设置器。

Try setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset) 尝试setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)

Refer to the LayerDrawable documentation for more help. 请参阅LayerDrawable文档以获取更多帮助。

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

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