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

本秂侑毒 提交于 2019-12-18 05:27:20

问题


I have the following 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.


回答1:


To make the loading smaller just make the images smaller.

Or use RelativeLayout and put the loading in an ImageView and scale it.

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

android:drawablePadding="3dp"



回答2:


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); 



回答3:


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

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



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/12767726/constrain-drawableleft-and-drawablerights-height-to-textviews-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!