ListView 中的居中文本项

2023-12-27

如何水平居中对齐文本项ListView in my Layout?
老实说,在问这样一个基本问题之前,我在谷歌上搜索了至少一个小时。

Thanks.

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Remind..." />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/listviewTimeInterval"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:scrollbars="none" />

         <-- . . . --/>
         <-- more lists of the same kind--/>
         <-- . . .--/>

    </LinearLayout>

</LinearLayout>

您需要为列表视图项目创建自己的布局,如下所示

Example:

文本中心.xml:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
    <TextView
        android:id="@id/textItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

然后,在你的代码中,你必须这样做

ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.textcenter, R.id.textItem, functions);
listView.setAdapter(ad);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ListView 中的居中文本项 的相关文章