Skip to content Skip to sidebar Skip to footer

Android, Vertical Alignment Of Button In Linear Layout

I'm trying to create a layout like the picture below, adaptive to screen size. Here is the my XML Source code:

Solution 1:

You should switch to GridLayout.

It's designed to do exactly what you need. It's based on rows and columns and you can specify the row and col spans of elements. Not to mention that it will give you better performance then nesting LinearLayouts.

Solution 2:

You should change width of second LinearLayout and also remove android:width attribute from those buttons

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:orientation="horizontal"android:minWidth="25px"android:minHeight="25px"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/LnrLytRow1"android:weightSum="100"android:layout_marginLeft="25dp"android:layout_marginRight="25dp"><Buttonandroid:text="Button1"android:layout_width="wrap_content"android:layout_height="match_parent"android:id="@+id/button1"android:width="0dp"android:layout_weight="25" /><Buttonandroid:text="Button2"android:layout_width="wrap_content"android:layout_height="match_parent"android:id="@+id/button2"android:width="0dp"android:layout_weight="25" /><Buttonandroid:text="Button3"android:layout_width="wrap_content"android:layout_height="match_parent"android:id="@+id/button3"android:width="0dp"android:layout_weight="25" /><Buttonandroid:text="Button4"android:layout_width="wrap_content"android:layout_height="match_parent"android:id="@+id/button4"android:width="0dp"android:layout_weight="25" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:minWidth="25px"android:minHeight="25px"android:id="@+id/LnrLytRow2"android:weightSum="100"android:layout_marginRight="25dp"android:layout_marginLeft="25dp"><Buttonandroid:text="Button5"android:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button5"android:layout_weight="25" /><Buttonandroid:text="Button6"android:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button6"android:layout_weight="25" /><Buttonandroid:text="Button7"android:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button7"android:layout_weight="50" /></LinearLayout>

Solution 3:

In your build.gradle (any versions may be ok):

compile"com.android.support:gridlayout-v7:+"

Copy this code in your xml file

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"xmlns:app="http://schemas.android.com/apk/res-auto"app:columnCount="4"app:rowCount="2"><Buttonapp:layout_columnWeight="1"app:layout_rowWeight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1"/><Buttonapp:layout_columnWeight="1"app:layout_rowWeight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2"/><Buttonapp:layout_columnWeight="1"app:layout_rowWeight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3"/><Buttonapp:layout_columnWeight="1"app:layout_rowWeight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button4"/><Buttonapp:layout_columnWeight="1"app:layout_rowWeight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button5"/><Buttonapp:layout_columnWeight="1"app:layout_rowWeight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button6"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button7"app:layout_columnWeight="1"app:layout_rowWeight="1"app:layout_columnSpan="2"/></android.support.v7.widget.GridLayout>

SEE PROOF

enter image description here

Post a Comment for "Android, Vertical Alignment Of Button In Linear Layout"