【发布时间】:2013-12-10 04:45:17
【问题描述】:
我试图让 ListView 的内容在对话框中显示时居中。 这是我显示 ListView 的代码
String[] myItems = {"item1", "item2", "item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.setting_layout, myItems);
ListView list = (ListView) deleteDialog.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setFadingEdgeLength(5);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) {
// TODO Auto-generated method stub
}
}
这是 xml 部分:
TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:textSize="25sp"
这是删除对话框的布局:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:dividerHeight="5dp"
android:gravity="center">
</ListView>
它正在运行,但内容始终显示在左侧。 我试过这个:How to set the text at center in ListView android application? 但它没有用。
希望有人能指导我。
【问题讨论】:
-
如果将
android:gravity="center"设置为ListView 会发生什么? -
其实我没试过。问题是当我编写 android 时:它开始提供我可以使用的选项,而重力不在其中,所以我只是假设我不能使用。我刚试了一下,没有报错,但还是给左边的物品
-
将您的线性布局的宽度更改为 fill_parent 而不是 wrap_content
-
如果你把它写成答案,我会接受它。
-
欢迎我把它写成答案