Android 带有列表视图和消息的对话框
我需要创建一个包含ListView和message的对话框,但是根据我的理解,使用标准的AlertDialog是不可能的。所以我决定用文本和列表视图创建自定义视图,并将其附加到对话框 但是,我的列表视图是空的。以下是java代码:Android 带有列表视图和消息的对话框,android,listview,dialog,android-alertdialog,Android,Listview,Dialog,Android Alertdialog,我需要创建一个包含ListView和message的对话框,但是根据我的理解,使用标准的AlertDialog是不可能的。所以我决定用文本和列表视图创建自定义视图,并将其附加到对话框 但是,我的列表视图是空的。以下是java代码: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Hello, title!"); LayoutInflater factory =
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hello, title!");
LayoutInflater factory = LayoutInflater.from(this);
View content = factory.inflate(R.layout.dialog, null);
ListView lv = (ListView) content.findViewById(R.id.list);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, ITEMS));
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
builder.setView(content).setPositiveButton("OK", this).setNegativeButton("Cancel", this);
AlertDialog alert = builder.create();
alert.show();
下面是对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, text!" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
></ListView>
</LinearLayout>
结果如下:
非常感谢您的帮助。谢谢 在linearlayout中缺少
android:orientation=“vertical”
您的xml将是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, text!" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
></ListView>
</LinearLayout>
设置方向是垂直的,就像
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, text!" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
></ListView>
</LinearLayout>
在您的布局中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, text!" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
></ListView>
</LinearLayout>