2

I have a ListView which has two columns and what I want is to center that ListView in a Linerlayout. Here is the layout code of the ListView.

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

And here is the layout of the individual items of the ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView 
        android:id="@+id/prayLabel"
        android:layout_width="100dip"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/prayValue"
        android:layout_width="100dip"
        android:layout_height="wrap_content" />
</LinearLayout>

Although my ListView is appearing vertically centered, but it's not appearing horizontally centered as its width is spanning the entire width of the screen. I guess as I used wrap_content in the layout_width in all the places its width should not span the entire width of the screen/layout?

Thanks

4

2 に答える 2

0

次のように、線形レイアウトで使用android:weightSumし、両側の空のビューを使用して中央に配置できますListView

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

もちろん、重みを調整することもできますがViews、同じように設定されていることを確認して、中央に配置してListViewください。

于 2016-09-09T14:13:40.980 に答える