Android listView在RelativeLayout中的中心位置
我有一个RelativeLayout,在这个RelativeLayout中也有listview。我希望我的列表视图是中心位置。listivew将从屏幕中心开始 这是我的xml代码Android listView在RelativeLayout中的中心位置,android,listview,android-activity,android-relativelayout,Android,Listview,Android Activity,Android Relativelayout,我有一个RelativeLayout,在这个RelativeLayout中也有listview。我希望我的列表视图是中心位置。listivew将从屏幕中心开始 这是我的xml代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:o
<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>
您可以在ListView属性中使用以下内容:
android:layout_centerInParent="true"
付诸表决:
在主RelativeLayout中。首先,如果此xml中没有其他元素,那么只需要一个RelativeLayout作为父对象,也就足够了。
<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>
下面是两个解决方案,您可以使用其中任何一个来满足您的需求
方法1
方法2
试试android:布局\重力=中心\垂直\中心\水平为什么你有两个相对位置。此代码可能会导致XML文件出错:中止生成。无论如何,要回答您的问题,relativelayout中有一些属性,如centerInParent、Gravity、Gravity\u layout等@PedroHawk未更改。我添加了您的codelayout\u Gravivty不适用于relativelayout@你说得对。我的错误。这不是解决方案。你能给我看一下所有的xml代码吗?这不是解决办法,而是解决办法。我已经告诉你该怎么做了。将该单一属性添加到ListView中的属性。在你说这不是一个解决方案之前,先尝试一下。
<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>
<?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>
<?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>