Android 将 drawableLeft 和 drawableRight 的高度限制为 TextView 的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12767726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:02:13  来源:igfitidea点击:

Constrain drawableLeft and drawableRight's height to TextView's height

androidtextviewdrawable

提问by Dzhuneyt

I have the following TextView:

我有以下几点TextView

<TextView
    android:drawableLeft="@drawable/loading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Updating Rates" />

And the result is this:

结果是这样的:

How do I make the loading circle drawable constrain proportionally so its height matches the height of the text?

如何使加载圆可绘制成比例约束,使其高度与文本高度匹配?

Also, if possible, I would like to add some spacing between the text and the image.

另外,如果可能的话,我想在文本和图像之间添加一些间距。

采纳答案by JafarKhQ

To make the loadingsmaller just make the images smaller.

要使loading更小,只需使图像更小。

Or use RelativeLayoutand put the loading in an ImageViewand scale it.

或者使用RelativeLayout并将负载放入ImageView并缩放它。

Or you can add padding between the drawables and the text using android:drawablePadding(or setCompoundDrawablePadding()):

或者您可以使用android:drawablePadding(或setCompoundDrawablePadding())在可绘制对象和文本之间添加填充:

android:drawablePadding="3dp"

回答by Ravi

I think I found the solution, but a bit late

我想我找到了解决方案,但有点晚了

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5),(int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 

回答by Dmide

Ravi's solution worked for me, but on some devices drawable was disappearing. Fixed this by removing the scaling.

Ravi 的解决方案对我有用,但在某些设备上 drawable 正在消失。通过删除缩放来修复此问题。

    int bound = (int) (drawable.getIntrinsicWidth() * 0.5);
    drawable.setBounds(0, 0, bound, bound);
    b.setCompoundDrawables(drawable, null, null, null);

回答by Abdul Vajid

A Better solution will to create a XML in drawable folder and add the image to this XML, and maintain scaleType as "fitXY". Now you can make this XML as the drawableLeft.

更好的解决方案是在 drawable 文件夹中创建一个 XML 并将图像添加到此 XML,并将 scaleType 保持为“fitXY”。现在您可以将此 XML 设为 drawableLeft。

回答by jay

You just need to declare two separate views imageview and textview, and set them accordingly

您只需要声明两个单独的视图 imageview 和 textview,并相应地设置它们