Set actual text padding in TextView with compound drawables

ぐ巨炮叔叔 提交于 2019-12-04 15:46:38

问题


I have a ListView populated with an ArrayAdapter. For items I use just a single TextView layout. I want some of the rows to have compound drawables set.

Question: is there a way to set padding for the actual text that is contained in TextView so that the compound drawables don't get the padding too? Other solution would be to lock the width of text. Do I need to add ImageViews to my layout?

Thanks in advance.


回答1:


Quite simple

android:drawablePadding="5dp"

It will automatically use the padding according to the direction




回答2:


I'm posting this as an answer so that someone, who comes across this post finds the answer.

There's no direct way to set a padding only to text, but you can set a positive padding to the whole TextView and set a negative padding for the drawable.




回答3:


Improving on Gio's sugestion, which is probably for a dated API/SDK...

TL:DR - just apply android:drawablePadding to increase distance between image and text.

Explanation: android:drawablePadding now applies padding between CompoundDrawable and original View, in this case the TextView containing the text;

android:padding and modifiers now apply to the whole group, including all compounds.

Doing the negative-positive hack doesn't seem to work no more.

So depending on the drawable position (drawableTop/Left/...), the padding will apply in the opposite direction of it, right next to the drawable, in other words between the two elements. For instance, Applying drawablePadding="10dp" to a left placed drawable would have a similar effect as setting an individual ImageView to the left of the TextView with paddingRight="10dp" (at least padding-wise).



来源:https://stackoverflow.com/questions/12455514/set-actual-text-padding-in-textview-with-compound-drawables

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