2011-02-07 14 views

答えて

109

使用

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

は(ドローアブルを設定します。テキストの左、上、右、下に表示されます)。そこにDrawableを必要としない場合は0を使います。 Drawablesの境界は、その本来の境界に設定されます。

あなたは

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

を使用している場合、およびテキストの下の右側に、上記の左側に表示されるように(もしあれば)ドローアブルを設定します。 Drawableが必要ない場合はnullを使用します。 DrawablesはすでにsetBounds(Rect)が呼び出されていなければなりません。

+0

このメソッドは非常に正しいですが、Drawableの代わりにResource idを設定したいと思います。同じことをする方法はありますか? – Maneesh

+3

はい。リソースのリソース= getResources(); 描画可能なdrawable = resources.getDrawable(id); –

+0

Tanmay、私もこれをやろうとしていますが、コードを使ってdrawableTopを設定する方法がまだ分かりません。私はdrawableを得る方法を見ていますが、それをボタンにどのように設定するのですか? – Otto

2
 Button button = (Button) findViewById(R.id.button); 
     button.setCompoundDrawables(left, top, right, bottom); 
+0

方法は非常に正しいですが、私は描画可能の代わりにリソースIDを設定します。同じことをする方法はありますか? – Maneesh

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); 
+7

-1 'drawableTop'ではなく、背景イメージを設定します! –

31
Drawable top = getResources().getDrawable(R.drawable.image); 
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null); 
+0

これはみんな働いています、ありがとう –

関連する問題