ListView 中的内容未居中

Contents in ListView not centered

我的应用程序从数据库中检索用户详细信息并显示在 ListView 中。但是ListView中显示的内容格式不正确

这是我的布局当前呈现的方式:

这是我的布局文件 activity:

<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"
    android:background="#000000"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".LeaderBoard" >

    <LinearLayout
        android:id="@+id/column"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/column_header1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/strLBoardHeaderRank"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:text="@string/strLBoardHeaderName"
            android:layout_gravity="center"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/strLBoardHeaderScore"
            android:textColor="#f00"
            android:textSize="24sp" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/column" >
    </ListView>

</RelativeLayout>

列表 activity 的布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <TextView
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:textColor="#f00"
        android:textSize="20sp" />

</LinearLayout>

为所有 textview 添加 android:gravity="center" 并为列表 activity 添加布局文件 textview 由于重量 [=24],宽度应为 0dp =] 你还应该添加 android:orientation="horizontal"

<TextView
            android:id="@+id/column_header1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:gravity="center"
            android:text="strLBoardHeaderRank"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:gravity="center"
            android:text="strLBoardHeaderName"
            android:layout_gravity="center"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:gravity="center"
            android:text="strLBoardHeaderScore"
            android:textColor="#f00"
            android:textSize="24sp" />

列表的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/rank"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:gravity="center"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:textColor="#f00"
        android:textSize="20sp" />

</LinearLayout>

试试这个,对我有用

//Main Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="horizontal" >
<TableLayout
                    android:id="@+id/add_rule_table"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:layout_marginTop="5dp"
                    android:background="@color/list_divider"
                    android:orientation="vertical"
                    android:stretchColumns="*"
                    android:visibility="gone" >

                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/list_divider"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@color/list_divider"
                            android:ems="4"
                            android:gravity="center"
                            android:text="@string/rule_no"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@color/list_divider"
                            android:ems="4"
                            android:gravity="center"
                            android:text="@string/lot"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@color/list_divider"
                            android:ems="4"
                            android:gravity="center"
                            android:text="@string/discount"
                            android:textSize="12sp"
                           android:textStyle="bold" />


                    </TableRow>

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:background="@color/list_divider" />
                </TableLayout>


                    <ListView
                        android:id="@+id/rule_list_view"
                        android:layout_width="0dip"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="2dp"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:cacheColorHint="#00000000"
                        android:choiceMode="none"
                        android:listSelector="@android:color/transparent" />

            </LinearLayout>


//Custom List View Items

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
  >

    <TableLayout
        android:id="@+id/add_rule_table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:layout_marginTop="5dp"
        android:background="@color/white"
        android:orientation="vertical"
        android:stretchColumns="*" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/rule_nos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_marginRight="1dp"
                android:ems="3"
                android:gravity="center_horizontal"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="1"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lots"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="4"
                android:gravity="center"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="23"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/discount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="4"
                android:gravity="center"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="34%"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/edit_rule"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/edit_rule"
                android:ems="4"
                android:gravity="center_horizontal"
                android:text="@string/edit"
                android:textColor="#ffffff"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/delete_rule"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:background="@drawable/delete_rule"
                android:ems="4"
                android:gravity="center_horizontal"
                android:text="@string/delete"
                android:textColor="#ffffff"
                android:textSize="12sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>