有可能创build对话框内的列表视图?

我创build了一个自定义对话框类

public class NewPost extends Dialog { // functionality } 

现在我的要求是在里面创buildlistview。 我知道我们可以在里面创build文本框,button,下拉列表。

但为了创build列表视图,我们应该从listActivity类inheritance我们的类

你的build议是可能或不可以,如果是的话,如何实现这个使用任何接口或什么?

您不必为了使用列表视图而扩展listActivity

扩展listActivity会给你一些免费的function,如getListView() (如果我记得正确的方法名),但也可以像使用其他视图一样手动使用findViewById()来完成。

这个实现不需要你做任何的xml布局。 它在“onCreateDialog”覆盖中被写为一个case语句,但是如果为了您的目的,您可以很容易地进行调整:

 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select Color Mode"); ListView modeList = new ListView(this); String[] stringArray = new String[] { "Bright Mode", "Normal Mode" }; ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray); modeList.setAdapter(modeAdapter); builder.setView(modeList); final Dialog dialog = builder.create(); dialog.show(); 

是。

您始终可以在对话框中使用ListView。 而且你绝对不需要ListActivity来创buildListView。

代码可能是这样的:

 Dialog dlg = new Dialog(context); LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = li.inflate(R.layout.my_layout, null, false); dlg.setContentView(v); dlg.show(); 

my_layout.xml:

 <ScrollView xmlns:android="blah" android:id="xid" android:layout_height="h" android:layout_width="w"> <ListView blah blah blah attributes /> </ScrollView> 

最简单的方法:

  ListView listView = new ListView(this); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"item 1", "item 2", "item 3"})); Dialog dialog = new Dialog(this); dialog.setContentView(listView); dialog.show(); 

您可以使用此布局创build自定义对话框:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:background="@drawable/dialogs"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:paddingTop="10dip"> <ImageView android:id="@+id/dialog_title_image" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/info"/> <TextView android:id="@+id/dialog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginTop="20dp" android:layout_centerInParent="true" android:text="Title" android:layout_toRightOf="@id/dialog_title_image" android:textColor="@android:color/black" android:textSize="20sp"/> <!-- Lista --> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="3dip" android:background="#1e90ff"/> <ListView android:id="@+id/component_list" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- Fine lista --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="10dip" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="bottom|center_horizontal" android:paddingBottom="20dip"> <Button android:id="@+id/positive_button" android:layout_alignParentLeft="true" android:layout_width="120dip" android:layout_height="wrap_content" android:background="#1e90ff" android:textColor="@android:color/white" android:text="@string/close"/> </RelativeLayout> </LinearLayout> 

也可以为每个项目创build一个自定义布局,如果你想:

 <?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" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="10dp" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textStyle="bold" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout> 

然后使用数组列表填充列表并设置视图:

  //creation and population of the list List<Component> my_list = new ArrayList<Component>(); createComponents(); //adapter array_adapter = new ComponentAdapter(context, R.layout.component,my_list); //button to show the dialog Button button = (Button)findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub list_dialog = new Dialog(context); list_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); list_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); list_dialog.setContentView(R.layout.list_dialog); ListView list = (ListView)list_dialog.findViewById(R.id.component_list); list.setAdapter(array_adapter); Button positiveButton = (Button) list_dialog.findViewById(R.id.positive_button); positiveButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { list_dialog.dismiss(); } }); list_dialog.show(); } }); } 

参考文献: http : //pillsfromtheweb.blogspot.it/2014/10/android-listview-inside-alertdialog.html#links

结果: 在这里输入图像描述

您可以使用任何布局来提醒对话框。 如果你想要一个列表视图,我会这样做