363

メソッドを呼び出した後setCompoundDrawables、複合 Drawable は表示されません..

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);

何かご意見は?

4

10 に答える 10

73

これを使用してください(私はテストしました)。それはうまくいきます

Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
int h = image.getIntrinsicHeight(); 
int w = image.getIntrinsicWidth();   
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( image, null, null, null );
于 2014-10-19T08:03:04.537 に答える
51

境界が指定されていないため、画像は空白です。メソッドsetCompoundDrawables()を使用して、画像の境界を指定する前に使用できますが、Drawable.setBounds()

于 2012-02-13T07:20:28.890 に答える
47

上に設定した例:

view.setCompoundDrawablesWithIntrinsicBounds(
    null,
    getResources().getDrawable(R.drawable.some_img),
    null,
    null
);

引数の順序: (左、上、右、下)

于 2016-09-21T15:22:06.707 に答える
24

もう少し簡単に:

Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );
于 2015-07-10T16:44:17.813 に答える
11

API 22 で廃止されました。

このコードは私にとって便利です:

Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable, null, null, null);
于 2015-09-05T17:05:52.770 に答える
4

コトリンでは:

1) セットdrawable:

val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
    setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}

また

val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
    setBounds(0, 0, minimumWidth, minimumHeight)
}

2) セットTextView:

textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)

また

button.setCompoundDrawables(null, drawable, null, null)
于 2018-12-14T15:48:30.310 に答える