Serjaru
Serjaru

Reputation: 87

LinearLayout android layout_height

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

Upvotes: 2

Views: 2810

Answers (5)

Piyush
Piyush

Reputation: 18923

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>

Upvotes: 3

Jfreu
Jfreu

Reputation: 589

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)

Upvotes: 0

Jatin
Jatin

Reputation: 1670

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>

Upvotes: 0

Illegal Argument
Illegal Argument

Reputation: 10348

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>

Upvotes: 2

Ivo
Ivo

Reputation: 23357

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

Upvotes: 0

Related Questions