2013-03-11 46 views
2

<item android:left="10dp">的等效java代碼是什麼?Android什麼是`<item android:left =「10dp」>``的等價Java代碼?

更具體地講,我試圖讓一個位圖編程,等效於:

<item android:left="5dp" android:bottom="5dp"> 
    <bitmap 
     android:src="@drawable/screw" 
     android:gravity="bottom|left" /> 
</item> 

編輯

到目前爲止我:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = false; 
Bitmap screw = BitmapFactory.decodeResource(res, resId, options);  
BitmapDrawable s = new BitmapDrawable(res, screw); 
s.setGravity(Gravity.LEFT|Gravity.BOTTOM); 
+0

不錯的問題.. – Nezam 2013-03-11 05:25:42

+0

很確定您正在尋找LayoutParams進行定位。你能告訴我這個項目是什麼類型的視圖嗎? – 2013-03-11 06:59:04

+0

@LarryMcKenzie它是'' – ilomambo 2013-03-11 07:01:28

回答

1

你可以膨脹從xml的視圖很容易,或者如果您以編程方式加載drawable,則應使用文檔:http://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html

自從我使用java/android之後,它已經有一段時間了,但是像這樣。

Drawable[] layers = [drawable1, drawable2, etc] 
LayerDrawable mDrawable = new LayerDrawable(layers); 
//mDrawable.setLayerInset(int index, int left, int top, int right, int bottom) 
//you will have to manually calculate density pixels. 
mDrawable.setLayerInset(0, 5, 0, 0, 5) 
+0

中的一項內容但是,您正在設置LayerDrawable上的插圖,而不是特定的項目。 :::如果我有兩個物品,每個都有不同的插入物,該怎麼辦? ::: BitmapDrawable類沒有'setLayerInset()'方法或類似方法,也沒有Bitmap類。 – ilomambo 2013-03-11 07:51:37

+1

再看一次。 ['setLayerInset']的第一個參數(http://goo.gl/zqzMm)是項目索引。 – 2013-03-11 09:04:15

+1

@ilomambo正如Jeremy所說,你使用數組中的drawable的索引來指定插入。因此,第一個mDrawable.setLayerInset(0,5,0,0,5),第二個mDrawable.setLayerInset(1,10,0,0,10)等 – 2013-03-11 15:16:54

相關問題