Android 带有ListView的自定义对话框
我正试图让一个对话框弹出一个评论列表,并在底部选择自己添加一个。(对于corse,如果有填充的列表,我想隐藏txtfirstComment,但我还没有隐藏它) 以下是我迄今为止所做的工作:Android 带有ListView的自定义对话框,android,Android,我正试图让一个对话框弹出一个评论列表,并在底部选择自己添加一个。(对于corse,如果有填充的列表,我想隐藏txtfirstComment,但我还没有隐藏它) 以下是我迄今为止所做的工作: public static Dialog openComments(Activity a) { // custom dialog final Dialog dialog = new Dialog(a); dialog.getWindow().setBackgroundDrawable(
public static Dialog openComments(Activity a) {
// custom dialog
final Dialog dialog = new Dialog(a);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations = R.style.dialog_animation;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.feed_comment);
String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(a, R.id.lstCommentList, android.R.id.text1, listItems);
setListAdapter(adapter);
return dialog;
}
公共静态对话框openComments(活动a){
//自定义对话框
最终对话框=新对话框(a);
dialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations=R.style.dialog\u动画;
对话框.requestWindowFeature(窗口.FEATURE\u无\u标题);
setContentView(R.layout.feed_comment);
字符串[]listItems={“项目1”、“项目2”、“列表”、“android”、“项目3”、“foobar”、“bar”、};
ArrayAdapter=新的ArrayAdapter(a,R.id.lstCommentList,android.R.id.text1,listItems);
setListAdapter(适配器);
返回对话框;
}
以下是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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/dialog_background"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical" >
<TextView
android:id="@+id/txtfirstComment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/nocomment"
android:background="#00000000"
android:gravity="center_horizontal"
android:textIsSelectable="false"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".1"
android:gravity="center_horizontal|center_vertical" >
<ListView
android:id="@+id/lstCommentList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:id="@+id/line1"
android:layout_width="wrap_content"
android:layout_height="1dip"
android:layout_marginTop="10dp"
android:background="@color/darkgrey"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical" >
<EditText
android:id="@+id/txtMakeComment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/makecomment"
android:background="#00000000"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
ArrayAdapter第二个构造函数参数表示您正在传递listview的资源ID,而它应该是包含textview(第三个参数)的布局(Android.R.layout.simple_list_item_1)的ID
您是否也在使用ListActivity?您的问题是什么?不要使用独立对话框,而是使用
DialogFragment
。这是一个很好的教程,如果你在谷歌上搜索“android自定义对话框”,你可以找到它。本教程没有解决我显示listview的问题。我无法使listview正常工作您应该使用警报对话框…这是正确的方法。