Android listView在RelativeLayout中的中心位置

时间:2014-06-25 14:25:00

标签: android listview android-activity relativelayout

我有一个RelativeLayout,并且在这个RelativeLayout中也有listview。我希望我的listview成为中心位置。 listivew将从屏幕中心开始 这是我的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff0000" >

<RelativeLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="#000000" >

    <ListView
        android:id="@+id/menu_content_menulist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:divider="@null" >
    </ListView>
</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

您可以在ListView属性中使用以下内容:

android:layout_centerInParent="true"

答案 1 :(得分:1)

首先,你只需要一个RelativeLayout作为父级(如果你在这个xml中没有其他元素,那么ListView就足够了)。 以下是几种解决方案,您可以使用任何符合您要求的解决方案

方法1

<?xml version="1.0" encoding="utf-8"?>
<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" >

<ListView
    android:id="@+id/menu_content_menulist"
    android:layout_width="100dp"
    android:layout_height="300dp"
    <!-- Used centerInParent property to place the view in Centre of screen -->
    android:layout_centerInParent="true">
</ListView>

</RelativeLayout>

方法2

<?xml version="1.0" encoding="utf-8"?>
<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"
<!-- set the gravity of parent element to center which is applied to its child elements -->
android:gravity="center" >

<ListView
    android:id="@+id/menu_content_menulist"
    android:layout_width="100dp"
    android:layout_height="300dp" >
</ListView>

</RelativeLayout>

答案 2 :(得分:0)

把:

android:gravity="center"

在您的主要RelativeLayout。

答案 3 :(得分:0)

  <ListView
    android:id="@+id/menu_content_menulist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:divider="@null" >
</ListView>