Get available width for TextView with layout_width=“wrap_content”

一世执手 提交于 2019-12-11 05:34:40

问题


I have a complex layout that has TextView with layout_width="wrap_content". I want to get maximum width that this TextView can fill.

Is there a universal way to do it? Or should I go over all parents and calculate their margins, paddings, etc? How does Android do it?


回答1:


You can get it's parent and calculate it's width like this. This width would be the maximum width the TextView can take provided the parent has no padding.

View parent = (View) textView.getParent();
int width = parent.getWidth();

If the parent has some padding you'll need to compute it separately and subtract from this width.

int paddingE = parent.getPaddingEnd();
int paddingS = parent.getPaddingStart();
int totalWidth = width - (paddingE + paddingS);


来源:https://stackoverflow.com/questions/45858874/get-available-width-for-textview-with-layout-width-wrap-content

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