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

时间:2014-09-04 12:21:19

标签: java android

给出了以下层列表的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>

要动态地执行某些操作,我需要以编程方式将此代码重建为LayerDrawable类。此代码存在:

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

如何将android:left="5dp"翻译成Java代码?这个属性没有设置者。

1 个答案:

答案 0 :(得分:1)

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

有关更多帮助,请参阅LayerDrawable documentation