How can I give all elements fixed width inside LinearLayout
?Imagine I have a list of buttons and I would to center them all and also give them same width based on the element with the biggest width, is there any way how to make the layout manager to do this dynamically (I mean don't repeat the code for every button)?My code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button android:id="@+id/mainStageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="menuClickListener"
android:text="@string/mainStage"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="menuClickListener"
android:text="@string/amphiteatre"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="menuClickListener"
android:text="@string/aula" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="menuClickListener"
android:text="@string/mensa"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="menuClickListener"
android:text="@string/sportsSchedule"/>
Have you consider using a listView? You could achieve the results that you are looking for with a listView with a much cleaner code-base:
You could use a for-loop and loop trough all the Layout's children via view. getChildAt(index)
view. getChildAt(index)
:)
The most reliable method:
Write a custom layout manager
Or even easier, pick an existing layout manager that does what you need
In this case TableLayout
is a subclass of LinearLayout
that already has the right properties.
From the link:
The width of a column is defined by the row with the widest cell in that column
By just replacing the linear layout with this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is rather long" >
</Button>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium" >
</Button>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Short" >
</Button>
</TableRow>
</TableLayout>
The results are like this image:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.