将内容视图添加到对话框

时间:2014-03-31 17:33:38

标签: android listview

我尝试在对话框中添加EdittextListView。 我使用dialog.addContentView(lstcontent,params)方法向对话框添加视图。

这是我的代码:

dialog = new Dialog(MainActivity.this);

final LinearLayout.LayoutParams lp = new
LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

dialog.addContentView(edittext, lp);
dialog.addContentView(listview,lp);

dialog.show();

但我的listview在edittext上。我想在listview上面设置edittext。

here is capture of screen

我也是这样使用的:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
LinearLayout layout = new LinearLayout(G.context, (AttributeSet) params);

layout.addView(input);
layout.addView(lstcontent);
dialog.setcontentView(layout);

但这个崩溃的应用程序:     将LayoutParams强制转换为android.util.Attributeset

3 个答案:

答案 0 :(得分:0)

使用dialog.setContentView(R.layout.yourlayout)并使用您自己的布局

可能会更容易

答案 1 :(得分:0)

您可以使用相对布局,例如

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

然后

   params.addRule(RelativeLayout.BELOW, id);

了解更多信息Programatically add view one below other in relative layout

<强> //修改

RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new 
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params1.addRule(RelativeLayout.ABOVE, LISTVIEW_ID);
params2.addRule(RelativeLayout.BELOW, EDITTEXT_INPUT_ID);

layout.addView(EDITTEXTID, params1);
layout.addView(LISTVIEW, params2);

现在您必须为 DIALOG 设置此布局,它应该可以正常工作

答案 2 :(得分:0)

我找到了解决这个问题的新方法。

LayoutInflater inflate2 = ((Activity) G.context).getLayoutInflater();
View dialogView = inflate2.inflate(R.layout.layout_dialog, null);

input = (EditText) dialogView.findViewById(R.id.edt1);
lstcontent = (ListView) dialogView.findViewById(R.id.lst1);

lstcontent.setAdapter(adapter); 

这是layout_dialog.xml

<?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:orientation="vertical" >
<EditText 
    android:id="@+id/edt1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lst1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >    
</ListView>