Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout - Vertically not align

I got a problem with LinearLayout on Android. I have four buttons. Each button has a fixed size, but the text can vary in length.

My problem is that they are not align with the top of each. They seen to be align with the top of the text inside of each botton which change depending on the number of line there is inside the button (See picture).

Also, I want to keep using LinearLayout as I will eventually use code will add buttons based on data from a database.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
       <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
           <Button android:text="Line1 Line2" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1 Line2 Line3" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
       </LinearLayout>

</LinearLayout>

http://i.imgur.com/0Kj8h.png

EDIT: ANSWER (Can't answer my own question):

Ok, I just found the answer by myself. You have to add android:baselineAligned="false" to LinearLayout or any other similar control that could show the same behavior.

You can also fix this in the UI designer using the button called "Toggle Baseline Alignment".

So the resulting code is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
       <LinearLayout android:baselineAligned="false" android:layout_width="match_parent" android:layout_height="match_parent">
           <Button android:text="Line1 Line2" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1 Line2 Line3" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
       </LinearLayout>

</LinearLayout>
like image 890
darkzangel Avatar asked Jul 27 '11 04:07

darkzangel


People also ask

How do you do a vertical LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How to center LinearLayout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

What is orientation property in LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.


1 Answers

its better to use RelativeLayout for such type of alignment, as RelativeLayout's concept says that it "reference" of a control

like image 96
Mohammed Azharuddin Shaikh Avatar answered Nov 02 '22 23:11

Mohammed Azharuddin Shaikh