在我调用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);
任何想法吗?
当前回答
我需要使用setCompoundDrawablesWithIntrinsicBounds。
其他回答
示例设置在顶部:
view.setCompoundDrawablesWithIntrinsicBounds(
null,
getResources().getDrawable(R.drawable.some_img),
null,
null
);
参数顺序:(左,上,右,下)
Kotlin的例子:
val myView = layoutInflater.inflate(R.layout.my_view, null) as TextView
myView.setCompoundDrawablesWithIntrinsicBounds(0, myDrawable, 0, 0)
我需要使用setCompoundDrawablesWithIntrinsicBounds。
图像是空白的,因为它没有指定的边界。你可以使用setCompoundDrawables(),但在指定图像的边界之前,使用Drawable.setBounds()方法
在芬兰湾的科特林:
1)设置可绘制:
val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
or
val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
setBounds(0, 0, minimumWidth, minimumHeight)
}
2)设置TextView:
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
or
button.setCompoundDrawables(null, drawable, null, null)