问题
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