当我添加如下图标时:
etComment = (EditText) findViewById(R.id.et_comment);
Drawable img = getResources().getDrawable( R.drawable.warning );
etComment.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
![enter image description here](https://i.stack.imgur.com/JPEs0.png)
图标会调整 EditText 的大小。如何计算img大小并将其放入EditText而不调整EditText大小?
Thanks!
放克僧侣
使用 setCompounDrawables() 而不是 setCompoundDrawablesWithIntrinsicBounds() - 您必须手动设置可绘制对象的边界。
我不明白如何手动计算边界。我有 EditText 的高度和宽度:
etComment = (EditText) findViewById(R.id.et_comment);
Drawable img = getResources().getDrawable( R.drawable.warning );
int size = etComment.getHeight();
img.setBounds(0, 0, size, size);
etComment.setCompoundDrawables( img, null, null, null );
但我在不同的屏幕尺寸下有不同的结果。如何计算图标的正确大小和填充?请你帮助我好吗?
我认为你可以针对不同的屏幕使用不同尺寸的图片,并使用 getMinimumWidth 来设置 Bounds。但是我之前没有尝试过,可能它不适合 .9 补丁。
当您使用 setCompoundDrawables 时,您需要如下代码:
Drawable img;
Resources res = getResources();
img = res.getDrawable(R.drawable.btn_img);
//You need to setBounds before setCompoundDrawables , or it couldn't display
img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
btn.setCompoundDrawables(img_off, null, null, null);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)