wawanopoulos
wawanopoulos

Reputation: 9794

Android : Place linearlayout on half of the height

Here is my app :

enter image description here

I would like to place a linearlayout (in blue) on the middle of the screen. (Half of height in fact).

I don't know how to do this.

Here is my code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E9E9E9"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="title app"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="70sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:background="@color/blue_dark_normal"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

    </LinearLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="bottom" >

         <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom"
             android:layout_marginLeft="15dp"
             android:layout_marginRight="5dp"
             android:layout_weight="1"
             android:orientation="vertical" >

             <Button
                 android:id="@+id/startact_btn_connect"
                 style="?android:attr/borderlessButtonStyle"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="10dp"
                 android:background="@drawable/bg_blue_button_login"
                 android:text="Se connecter"
                 android:textColor="@color/blanc" />

             <View
                 android:layout_width="fill_parent"
                 android:layout_height="3dp"
                 android:layout_marginBottom="10dp"
                 android:background="@color/blue_pressed" />
         </LinearLayout>

         <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom"
             android:layout_marginLeft="5dp"
             android:layout_marginRight="15dp"
             android:layout_weight="1"
             android:orientation="vertical" >

             <Button
                 android:id="@+id/startact_btn_view_annonces"
                 style="?android:attr/borderlessButtonStyle"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="10dp"
                 android:background="@drawable/bg_blue_dark_button_login"
                 android:text="Voir les annonces"
                 android:textColor="@color/blanc" />

             <View
                 android:layout_width="fill_parent"
                 android:layout_height="3dp"
                 android:layout_marginBottom="10dp"
                 android:background="@color/blue_dark_pressed" />
         </LinearLayout>

     </LinearLayout>

</LinearLayout>

Upvotes: 0

Views: 254

Answers (3)

Farouk Touzi
Farouk Touzi

Reputation: 3456

Try this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E9E9E9"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/txt_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:text="title app"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="70sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:background="@color/blue_dark_normal"
    android:layout_centerInParent="true"
    android:orientation="vertical" >

</LinearLayout>

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_alignParentBottom="true" >

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         android:layout_marginLeft="15dp"
         android:layout_marginRight="5dp"
         android:layout_weight="1"
         android:orientation="vertical" >

         <Button
             android:id="@+id/startact_btn_connect"
             style="?android:attr/borderlessButtonStyle"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
             android:background="@drawable/bg_blue_button_login"
             android:text="Se connecter"
             android:textColor="@color/blanc" />

         <View
             android:layout_width="fill_parent"
             android:layout_height="3dp"
             android:layout_marginBottom="10dp"
             android:background="@color/blue_pressed" />
     </LinearLayout>

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         android:layout_marginLeft="5dp"
         android:layout_marginRight="15dp"
         android:layout_weight="1"
         android:orientation="vertical" >

         <Button
             android:id="@+id/startact_btn_view_annonces"
             style="?android:attr/borderlessButtonStyle"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
             android:background="@drawable/bg_blue_dark_button_login"
             android:text="Voir les annonces"
             android:textColor="@color/blanc" />

         <View
             android:layout_width="fill_parent"
             android:layout_height="3dp"
             android:layout_marginBottom="10dp"
             android:background="@color/blue_dark_pressed" />
     </LinearLayout>

 </LinearLayout>

Upvotes: 0

Matt
Matt

Reputation: 3847

I believe you can do this with weights.

Here's my code to make a LinearLayout centered @ half height, in blue:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <FrameLayout
    android:id="@+id/topQuarterHeight"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp" />

  <LinearLayout
    android:background="#FF0000FF"
    android:orientation="vertical"
    android:id="@+id/midHalfHeight"
    android:layout_weight="2"
    android:layout_width="match_parent"
    android:layout_height="0dp"></LinearLayout>

  <FrameLayout
    android:id="@+id/bottomQuarterHeight"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp" />

</LinearLayout>

And here's what the preview looks like:

enter image description here

Upvotes: 0

FD_
FD_

Reputation: 12919

Use a RelativeLayout as the root, and simply add layout_centerInParent=true to the LinearLayout you want to center.

Upvotes: 1

Related Questions