I have three textViews that I want to align to the left, center, and right, respectively. The problem is that if the text is too large, then they begin to climb on each other (see screen). How to fix it?
1 answer
One solution is to use weights. To do this, the parent must specify the android:weightSum ="4" - this is the total amount under the elements (you can not specify if all elements have a weight size). For a TextView, specify its weight (for example) android:layout_weight="2" , as well as specify android:layout_width="0dp" . And so you can see more Here it is more clearly shown .
Here is not a big example. Where I have the first TextView weighing 2, and the other two weighing 1. (2 + 1 + 1). So the first one will occupy half the size of the parent, and the other two by quarter
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum ="4"> //Если указаны во всех подэлементов весы, не обязательно <TextView android:id="@+id/nickname_textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Никнэйм" /> <TextView android:id="@+id/battles_textView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Бои" android:textAlignment="viewEnd" /> <TextView android:id="@+id/wins_textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Победы" android:textAlignment="viewEnd" /> </TableRow> </LinearLayout> - You are absolutely right, corrected the mistake - Vitaly Robinovsky
- Thank you for what you need! - Ayaz Khusnutdinov
- it is better for you to fix the markup code itself and how to explain how the TableLayout component got into LinearLayot and why it is there. For example, I am not at all sure that such a markup will be inflated - pavlofff
- The fact is that I use several TableRows in this markup (each has a different number of elements). Is there a better way to place items? - Vitaly Robinovskiy
- The fact is that in this markup the
TableRowcomponent is at least absolutely superfluous and does not give anything but an additional load to the device, but in general it belongs to theTableLayoutcontainer, notLinearLayout. In essence,TableLayoutis a verticalLinearLayout, andTableRowis a horizontalLinearLayoutwith several additional table formatting attributes. For the layout of the table structure, this container is most suitable, but not in this example. In your answer there is a link to another answer, it contains a completely correct markup explaining the use of element weights. - pavlofff
|
