如何在ListView
中水平居中对齐Layout
的文字项?
老实说,在问这样一个基本问题之前,我用Google搜索了至少一个小时。
感谢。
<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>
答案 0 :(得分:15)
您需要为listview项创建自己的布局,如下所示
示例:
textcenter.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);
答案 1 :(得分:5)
如果定义了LisView本身,我发现无法将项目集中在ListView中 机器人:layout_width = “WRAP_CONTENT” 这不会产生,因为此参数应仅影响ListView的位置而不影响其元素。
无论如何,将列表的宽度更改为match_parent允许使用(layout_)重力属性将项目居中。
答案 2 :(得分:-4)
在自定义ListView xml文件的TextView布局中,使用android:gravity="center"
。那就行了。