列表项宽度与父对话框内不匹配

itz*_*har 5 android android-listview

我正在尝试使用对话框内的字符串对象创建一个简单的listView; 问题是列表项不是match_parent我设置的宽度:

在此输入图像描述

列表视图是根元素:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#ff0000"
    android:id="@+id/lvLinks"
    android:layout_height="match_parent"/ >
Run Code Online (Sandbox Code Playgroud)

项目清单:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="7dp"
    android:textSize="15sp"
    android:background="#0000ff"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

适配器:

ListView lv = (ListView) findViewById(R.id.lvLinks);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.list_item_links,items);
lv.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

我在很多操作系统上测试过它,这不是问题所在

固定: 回答

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff0000"
        android:id="@+id/lvLinks"
        android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ULH*_*TIL 9

为什么不将ListView放在LinearLayout或RelativeLayout中,其宽度为match_parent或fill_parent dialog.xml