在android中如何将列表视图居中放置在线性布局中

在android中如何将列表视图居中放置在线性布局中,android,android-layout,listview,Android,Android Layout,Listview,我有一个ListView,它有两列,我想要的是将ListView放在Linerlayout的中心。这是列表视图的布局代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_pa

我有一个
ListView
,它有两列,我想要的是将
ListView
放在
Linerlayout
的中心。这是
列表视图的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/mylist">
    </ListView>
</LinearLayout>
虽然我的
ListView
显示为垂直居中,但它没有显示为水平居中,因为它的宽度跨越了整个屏幕的宽度。我想当我在
版面宽度
中使用
wrap\u content
时,它的宽度不应该覆盖整个屏幕/版面的宽度


谢谢

为了简单起见,我会将其包装在一个
RelativeLayout
中-我也很难做到这一点,最后在更简单的布局上这样做**:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <RelativeLayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

            <!-- Note the 'centre in parent' tag below -->

            <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:id="@+id/mylist"/>

       </RelativeLayout>

    </LinearLayout>


**免责声明:在更复杂的视图上,此模式可能会变得昂贵,但是
线性布局
相对视图
列表视图
层次结构很好。

您可以在线性布局上使用
android:weightSum
,然后使用两侧的空视图将
列表视图居中,如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".05"/>
    <ListView
        android:id="@+id/list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".9"
        android:paddingLeft="4dp"
        android:paddingRight="4dp" />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".05"/>
</LinearLayout>


当然,您可以调整权重,但要确保
视图
设置相同,以便
列表视图
居中

将此添加到第二个xml
android:gravity=“center”
中layout@gtumca-MAC添加了这一功能,但没有任何区别:(.虽然它确实将内容放在ListView的中心,但ListView的宽度保持不变,即我不想要的是整个布局的宽度,我希望ListView的宽度减小到行的最大宽度(在我的例子中是200度)您是否尝试过将
RelativeLayout
包装
LinearLayout
wrap\u内容
?我总是以一种尝试和错误的方式来结束这一过程。如果这不起作用,那么在将RelativeLayout设置为包装\u内容后,将列表视图设置为
匹配\u parent
,即匹配Rel的宽度阿维拉约特。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".05"/>
    <ListView
        android:id="@+id/list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".9"
        android:paddingLeft="4dp"
        android:paddingRight="4dp" />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".05"/>
</LinearLayout>