使用setCompoundDrawables的EditText上计算时,图片大小 [英] Calculate image size when using setCompoundDrawables for EditText
问题描述
当我添加图标如下图所示:
When I am add icon like below:
etComment = (EditText) findViewById(R.id.et_comment);
Drawable img = getResources().getDrawable( R.drawable.warning );
etComment.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
图标调整大小的EditText。如何计算IMG大小和放入的EditText没有调整的EditText?
The icon resizes EditText. How can I calculate img size and put it into EditText without EditText resize?
谢谢!
FunkTheMonk
使用setCompounDrawables()而不是setCompoundDrawablesWithIntrinsicBounds() - 你必须手动设置可绘制的边界
FunkTheMonk
Use setCompounDrawables() instead of setCompoundDrawablesWithIntrinsicBounds() - you'll have to set the bounds of the drawables manually.
我不知道如何手动计算边界。我已经得到的EditText的高度和宽度:
I don't understand how to calculate Bounds manually. I have got height and width of 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 );
但我有不同的屏幕大小不同的结果。如何计算正确的大小和图标的填充?你能帮我吗?
but I have different results in different screen sizes. How I can calculate correct size and padding of icon? Could you please help me?
推荐答案
我认为你可以使用图片的不同大小不同的屏幕和使用getMinimumWidth设置Bounds.But我以前没尝试,可能是不恰当对于0.9补丁。
I think you can use different size of pics for different screens and use getMinimumWidth to set Bounds.But I did not try it before , may be it is not appropriate for .9 patch.
当您使用setCompoundDrawables,你需要code,如:
When you use setCompoundDrawables , you need code like :
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);
这篇关于使用setCompoundDrawables的EditText上计算时,图片大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!