Android 调用setCompoundDrawables()不会';t显示可拉伸的复合材料
调用该方法后,将不显示复合DrawableAndroid 调用setCompoundDrawables()不会';t显示可拉伸的复合材料,android,android-layout,android-drawable,Android,Android Layout,Android Drawable,调用该方法后,将不显示复合Drawable Drawable myDrawable = getResources().getDrawable(R.drawable.btn); btn.setCompoundDrawables(myDrawable, null, null, null); 有什么想法吗?我需要使用。图像是空白的,因为它没有指定的边界。您可以使用setCompoundDrawables(),但在指定图像边界之前,请使用Drawable.setBounds()方法使用此方法(我已测试
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);
有什么想法吗?我需要使用。图像是空白的,因为它没有指定的边界。您可以使用
setCompoundDrawables()
,但在指定图像边界之前,请使用Drawable.setBounds()
方法使用此方法(我已测试)。效果很好
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 );
再简单一点:
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);
示例设置为顶部:
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)
在科特林:
1) 设置可绘制的:
val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
或
2) 设置TextView
:
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
或
由于未指定边界,因此未显示图像,因此此处有2个选项
第一种方法
使用setCompoundDrawablesWithIntrinsicBounds
方法,如下所示
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn. setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null);
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
myDrawable.setBounds( 0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
btn.setCompoundDrawables(myDrawable, null, null, null);
第二种方法
在应用于TextView之前,可以将边界应用于可绘制对象,如下所示
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn. setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null);
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
myDrawable.setBounds( 0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
btn.setCompoundDrawables(myDrawable, null, null, null);
就是这样。正如下面的答案中所述,需要调用名为(..)的方法的变体,该方法具有intrinsicbounds
。另一方面,在这次调用之后,必须为复合绘图设置填充
,以产生效果。上面说:绘图必须已经调用过。嗨,hunterp,刚刚在咖啡店(Angel)见过你,现在我知道你知道安卓绘图是什么了(也许你在处理许多绘图时遇到了内存错误),我可以告诉你一些我合作过的项目必须解决这个问题,看看毕加索使用的(我合作的目的是让它更适合android)吧()@Dr1Ku实际上我以前有过它,而且它还是可以工作的。需要api 17如此可绘制。setBounds()可能会更好谢谢你。。这对我有用。。我可以知道这两者有什么区别吗?@user1324936“相对”版本需要API 17,其他版本可以与以前的版本一起使用versions@user1324936SetCompoundDrawableSwithinInstincBounds是在API级别3I中添加的,我使用SetCompoundDrawableRelativeWithinInstincBounds这在针对低于17的API时非常有用,由于EditText#setCompoundDrawablesWithIntrinsicBounds
至少需要API 17。您能提供这方面的源代码吗?我所看到的所有文档都表明这是最好的答案,因为你实际上提供了为什么挫折很重要的理由。@安迪,就是这样,我讨厌这些800票的顶级答案,它们只是复制粘贴了一行代码而没有任何字字字。为什么?对于删除一个可拖动?这应该是公认的答案,在我的情况下,使用按钮。它的工作正如预期!!!它还具有向后兼容性。对于TextviewSetCompoundDrawablesWithinInstincBounds
将起作用。。