Tenso
Tenso

Reputation: 92

Align TextView to top of RelativeLayout with ListView below

My TextView is aligning verticaly to the center of Layout, but I want it to be aligned to top, as it has to be header for ListView. I tried to put it in inner RelativeLayout, but it doesnt't help. My Layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/re/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#FC9"
android:gravity="center" >
<TextView
    android:id="@+id/main_activity_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/header_main_activity" 
    android:background="#3F51B5"
    android:textColor="#FFFFFF"
    />
</RelativeLayout>
    <ListView
    android:id="@+id/menu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/header"
    >
</ListView>
</RelativeLayout>

In editor everything looks good, but, when I run my app on the device and list isn't fully populated, my TextView together with ListView aligns to the vertical center.

What I have to do to make it align to top of the layout?

Upvotes: 0

Views: 2592

Answers (1)

harmanjd
harmanjd

Reputation: 1954

You have wrap_content for both the ListView and the RelativeLayout holding the TextView. One of those should be match parent. Also - you probably want the gravity to be "center_horizontal" not "center". Also add "layout_alignparentbottom" to your list view so that it starts at the bottom and is below the header.

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/re/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/main_activity_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/header_main_activity" 
    android:background="#3F51B5"
    android:textColor="#FFFFFF"
    />
    <ListView
    android:id="@+id/menu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/main_activity_header"
    android:layout_alignParentBottom="true"
    >
</ListView>
</RelativeLayout>

Upvotes: 1

Related Questions