メソッドを呼び出した後setCompoundDrawables
、複合 Drawable は表示されません..
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);
何かご意見は?
メソッドを呼び出した後setCompoundDrawables
、複合 Drawable は表示されません..
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);
何かご意見は?
これを使用してください(私はテストしました)。それはうまくいきます
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 );
境界が指定されていないため、画像は空白です。メソッドsetCompoundDrawables()
を使用して、画像の境界を指定する前に使用できますが、Drawable.setBounds()
上に設定した例:
view.setCompoundDrawablesWithIntrinsicBounds(
null,
getResources().getDrawable(R.drawable.some_img),
null,
null
);
引数の順序: (左、上、右、下)
もう少し簡単に:
Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );
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);
コトリンでは:
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)