Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout: layout_gravity="bottom" not working on Horizontal LinearLayout

Tags:

android

Ok, First of all, I searched all the internet, but nobody has a similar problem like this. So, all I want is to have 3 textViews, bottom aligned with the screen and with the same width. Here is an image representing what I want:

enter image description here

And here is my code:

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">  <LinearLayout        android:layout_width="match_parent"       android:layout_height="wrap_content"       android:layout_alignParentBottom="true">        <TextView             android:text="@string/help_1"            android:layout_weight="0.33"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@drawable/mynicebg1"             android:layout_gravity="bottom"/>        <TextView             android:text="@string/help_2"            android:layout_weight="0.33"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@drawable/mynicebg2"             android:layout_gravity="bottom"/>        <TextView             android:text="@string/help_3"            android:layout_weight="0.33"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@drawable/mynicebg3"             android:layout_gravity="bottom"/>   </LinearLayout>  </RelativeLayout> 

Well, it works when the 3 textViews have the same height, but when their size differ, I get the following result:

problem

Another strange behavior, is that when I set the layout_gravity of the biggest text to "center-vertical", I get the following result:

first workaround

So obviously, I went crazy and tried another combinations with center-vertical, but nothing worked as I wanted initially:

desperation workaround

So, any tips on how to solve this?

like image 352
Paulo Cesar Avatar asked Jul 04 '11 19:07

Paulo Cesar


People also ask

How do I fix the view to the bottom of LinearLayout?

You can set the layout_height="0dp" of your header, footer and ScrollView and define a layout_weight . Just play around with the values until you find out which works best. The resulting heights of header and footer would dynamically change with the screensize.

How do you do a horizontal 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" .

What is the default orientation of a LinearLayout?

The default orientation is horizontal.

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.


1 Answers

The Correct Answer

All the other answers are wrong. The important points:

  1. You don't need RelativeLayout. You can do this with just a LinearLayout.
  2. (Not critical but I guess you didn't know) Your weights don't need to sum to 1, you can just set them all to any equal value (e.g. 1).
  3. The critical thing is you need android:baselineAligned="false". I actually only found this by looking through the LinearLayout source. It is in the docs but they don't mention that it is on by default!

Anyway, here is the code:

<?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:baselineAligned="false">       <TextView             android:text="dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#eeffee"            android:layout_gravity="bottom"/>        <TextView             android:text="asd asd asd asd asd asd asd asd asd asd"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#eeeeff"            android:layout_gravity="bottom"/>         <TextView             android:text="qweoiu qweoiuqwe oiqwe qwoeiu qweoiu qweoiuq weoiuqw eoiquw eoiqwue oqiweu qowieu qowieu qoiweu qowieu qowieu qowieu qowieu qoiweu qowieu qoiwue "            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#ffeeee"            android:layout_gravity="bottom"/>   </LinearLayout> 

And how it looks:

Good linear layout

like image 97
Timmmm Avatar answered Sep 16 '22 19:09

Timmmm