2011-02-07 51 views

回答

109

使用按钮的

button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

设置可绘制(如果有的话)显示在文本的左侧,上方,右侧和下方。如果您不想在此处绘制Drawable,请使用0。 Drawables的边界将被设置为它们的内在边界。

如果使用

button.setCompoundDrawables(left, top, right, bottom);

设置可绘制(如果有的话)出现的,上面的左边,右边和下面的文字。如果您不想在此处绘制Drawable,请使用null。 Drawables必须已经有setBounds(Rect)调用。

+0

该方法非常正确,但我想设置资源ID代替drawable。有没有办法做同样的事情? – Maneesh 2011-02-07 09:46:53

+3

是。使用资源resources = getResources(); Drawable drawable = resources.getDrawable(id); – 2011-02-07 09:52:50

+0

Tanmay,我正在尝试这样做,我仍然不确定如何使用代码设置drawableTop。我看到如何得到一个可绘制的,但如何将它设置在按钮上? – Otto 2011-03-01 19:36:51

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); 
相关问题