![](/img/trans.png)
[英]How to align a View below a TextView in a RelativeLayout in Android
[英]Align TextView to top of RelativeLayout with ListView below
我的TextView垂直于Layout的中心对齐,但我希望它与顶部对齐,因为它必须是ListView的标题。 我试图将其放在内部的RelativeLayout中,但没有帮助。 我的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>
在编辑器中,一切看起来都不错,但是,当我在设备上运行我的应用程序并且列表没有完全填充时,我的TextView和ListView会对齐垂直中心。
我必须做些什么才能使其与布局的顶部对齐?
您既拥有ListView又拥有拥有TextView的RelativeLayout的wrap_content。 其中之一应该是配对父母。 另外-您可能希望重力为“ center_horizontal”而不是“ center”。 另外,将“ 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.