如何在運行時設置按鈕的財產「android:drawableTop
」如何設置屬性「機器人:drawableTop」在運行時
52
A
回答
109
使用按鈕的
button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
設置可繪製(如果有的話)顯示在文本的左側,上方,右側和下方。如果您不想在此處繪製Drawable,請使用0。 Drawables的邊界將被設置爲它們的內在邊界。
如果使用
button.setCompoundDrawables(left, top, right, bottom);
設置可繪製(如果有的話)出現的,上面的左邊,右邊和下面的文字。如果您不想在此處繪製Drawable,請使用null。 Drawables必須已經有setBounds(Rect)調用。
2
Button button = (Button) findViewById(R.id.button);
button.setCompoundDrawables(left, top, right, bottom);
+0
的方法是非常正確的,但我想設置資源ID到位繪製的。有沒有辦法做同樣的事情? – Maneesh 2011-02-07 09:44:52
20
final Drawable drawableTop = getResources().getDrawable(R.drawable.btn_check_buttonless_on);
btnByCust.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btnByCust.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
}
});
2
我使用此代碼使用的「Theme.Holo」按鈕,一個「自定義圖像」左邊,並用是從不同的方式調用的函數改變它(圖像)。
protected void app_dibujarLogojuego() {
if(bitmaplogojuego!=null){
bitmaplogojuego.recycle();
bitmaplogojuego=null;
}
Drawable LOGO = null;
if(verjuego.equals("COSA1")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA1); }
if(verjuego.equals("COSA2")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA2); }
if(verjuego.equals("COSA3")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA3); }
if(verjuego.equals("COSA4")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA4); }
BUTTON_DECLARED_ID.setCompoundDrawablesWithIntrinsicBounds(LOGO, null , null, null);
}
0
btn.setBackgroundResource(R.drawable.your_image_name_here);
31
Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
該方法非常正確,但我想設置資源ID代替drawable。有沒有辦法做同樣的事情? – Maneesh 2011-02-07 09:46:53
是。使用資源resources = getResources(); Drawable drawable = resources.getDrawable(id); – 2011-02-07 09:52:50
Tanmay,我正在嘗試這樣做,我仍然不確定如何使用代碼設置drawableTop。我看到如何得到一個可繪製的,但如何將它設置在按鈕上? – Otto 2011-03-01 19:36:51