简体   繁体   English

将TextView与下面的ListView对齐到RelativeLayout的顶部

[英]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. 我的TextView垂直于Layout的中心对齐,但我希望它与顶部对齐,因为它必须是ListView的标题。 I tried to put it in inner RelativeLayout, but it doesnt't help. 我试图将其放在内部的RelativeLayout中,但没有帮助。 My Layout.xml: 我的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. 在编辑器中,一切看起来都不错,但是,当我在设备上运行我的应用程序并且列表没有完全填充时,我的TextView和ListView会对齐垂直中心。

What I have to do to make it align to top of the layout? 我必须做些什么才能使其与布局的顶部对齐?

You have wrap_content for both the ListView and the RelativeLayout holding the TextView. 您既拥有ListView又拥有拥有TextView的RelativeLayout的wrap_content。 One of those should be match parent. 其中之一应该是配对父母。 Also - you probably want the gravity to be "center_horizontal" not "center". 另外-您可能希望重力为“ center_horizo​​ntal”而不是“ center”。 Also add "layout_alignparentbottom" to your list view so that it starts at the bottom and is below the header. 另外,将“ layout_alignparentbottom”添加到列表视图中,使其从底部开始并在标题下方。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM