2014-09-04 83 views
0

圖層列表的以下XML代碼IST給出:Android LayerDrawable:如何以編程方式設置圖層座標?

<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

您有'LayerDrawable'類的'setLayerInset()'方法。 – Luksprog 2014-09-04 12:24:13

+0

是的,但是使用此功能,這些值適用於所有圖層,而不僅僅是一個特定的圖層。 – EightBitBoy 2014-09-04 12:27:15

+1

嘗試'setLayerInset(layer,leftOffset,topOffset,rightOffset,bottomOffset)' – 2014-09-04 12:28:00

回答

相關問題