使用自定义布局在 ListFragment 顶部居中对齐 TextView

Center-align TextView on top of ListFragment with custom layout

a simple test app at GitHub I am trying to display a user avatar, then two TextViews Username and City (which should be center-aligned) and finally the main ListView of the ListFragment中:

在布局文件 fragment_account.xml 中,我尝试为两个 TextView 设置 android:textAlignment="center"(在上面的屏幕截图中用红色箭头标记),但是没有任何效果 - 可能是因为该应用程序适用于 minSdkLevel=9?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/avatar"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/photo"
        app:border_width="2dp"
        app:border_color="#FFF"/>

    <TextView                     <!-- how to center-align? -->
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Username"/>

    <TextView                     <!-- how to center-align? -->
        android:id="@+id/city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="City"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

请指教,如果有一个好的方法可以使这些标签居中对齐。

试试这个

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/avatar"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/photo"
        app:border_width="2dp"
        app:border_color="#FFF"/>

          <!-- add android:gravity="center" -->
    <TextView                     
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" 
        android:text="Username"/>

    <TextView   
        android:id="@+id/city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" 
        android:text="City"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>