LinearLayout android layout_height

2.5k Views Asked by At

Help me with XML layout.

I have three LinearLayout on activity, all LinearLayout have position vertical (from top to bottom)

My LinearLayout positions

  1. first LinearLayout have android:layout_height="30px", android:layout_width="fill_parent"
  2. second LinearLayout have android:layout_height="fill_parent", android:layout_width="fill_parent"
  3. third LinearLayout have android:layout_height="30px",android:layout_width="fill_parent"

But when second LinearLayout set as fill_parent it fill full screen (from first Layout to bottom of screen), and third LinearLayout cant display!

How i need fill the second layout?

Help me

5

There are 5 best solutions below

0
Illegal Argument On BEST ANSWER

You can use relative layout for your purpose:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout1"
    android:layout_above="@+id/linearLayout2"
    android:orientation="vertical" >
</LinearLayout>

</RelativeLayout>
0
Ivo On

The trick is to not use fill_parent for this but 0dp and give it a weight with android:layout_weight="1". This means that it will take all available extra space

0
Jatin On

You have to give weight to middle linear layout so it can take full height.

try this for middle linear layout,

   <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
    </LinearLayout>
0
Jfreu On

If you set height="fill_parent" to the middle layout, it will take the height of the parent view, so the 3rd view will not be visible (out of the screen)

1st layout : android:layout_height="30px", android:layout_width="match_parent"
2nd layout : android:layout_height="0dp", android:layout_width="match_parent", android:layout_weight="1"
3rd layout : android:layout_height="30px", android:layout_width="match_parent"

You should use "dp" instead of "px", to get the same size on different screen densities.

("fill_parent" is deprecated, you should use "match_parent", it does the same)

0
Piyush On

Use this one simply.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#fbfbfb"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:orientation="vertical" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal" />
</LinearLayout>
</LinearLayout>