how to align parent bottom in android linear layout?

Viewed 66689

I have a linearlayout

and I want to create at the bottom of it a slice.

I know there are some options, but I'm a bit confused

1) android:layout_gravity:"bottom" --> this doesn't work for me for some reason.

2) android:gravity_weight="0" and give the sibling before it android:gravity_weight:"1"

3) android:height="wrap_content" and give the sibling before it android:height:"match_parent"

I know how to do this using relativeLayout, but I want to practice linearLayout

what would you suggest?

<?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="match_parent"
    android:background="@color/blue_bg"
    android:orientation="vertical" >

   <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/signup_illu_why" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=""
        android:orientation="horizontal" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/signup_skip_icon" />

</LinearLayout>
7 Answers

Use:

android:gravity="bottom"

example:

<LinearLayout
  android:layout_weight="7"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:weightSum="5">
  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="some text"
     android:layout_weight="2"
     android:gravity="bottom" />
  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="some text"
     android:layout_weight="1" />
  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="some text"android:gravity="center"
     android:layout_weight="2" />
</LinearLayout>

Put Relative in LinearLayout and set Relative to matchparent, so you can use the layoutalignparentbottom

<?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="match_parent"
    android:background="@color/blue_bg"
    android:orientation="vertical" >

   <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/signup_illu_why" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=""
        android:orientation="horizontal" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/signup_skip_icon"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</LinearLayout>
Related