Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式添加的相对布局不工作_Android_Android Layout - Fatal编程技术网

Android 以编程方式添加的相对布局不工作

Android 以编程方式添加的相对布局不工作,android,android-layout,Android,Android Layout,我刚刚开始安卓编程,写了一段快速代码,但还没能让它做我想做的事情。基本上,我希望出现一个对话框,其中包含两个文本框和一个以特定布局显示的图像。我有以下代码: AlertDialog.Builder dialog = new AlertDialog.Builder(this); RelativeLayout dialogItems = new RelativeLayout(this); EditText itemTitle = new EditText(this); Ed

我刚刚开始安卓编程,写了一段快速代码,但还没能让它做我想做的事情。基本上,我希望出现一个对话框,其中包含两个文本框和一个以特定布局显示的图像。我有以下代码:

AlertDialog.Builder dialog = new AlertDialog.Builder(this);

    RelativeLayout dialogItems = new RelativeLayout(this);
    EditText itemTitle = new EditText(this);
    EditText itemBody = new EditText(this);
    ImageView dIcon = new ImageView(this);

    itemTitle.setText("Note Title");
    itemBody.setText("Note Details");
    dIcon.setImageResource(R.drawable.create);

    final RelativeLayout.LayoutParams imageParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    imageParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    imageParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    dIcon.setLayoutParams(imageParam);
    dialogItems.addView(dIcon, imageParam);

    final RelativeLayout.LayoutParams titleParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    titleParam.addRule(RelativeLayout.RIGHT_OF, dIcon.getId());
    titleParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    itemTitle.setLayoutParams(titleParam);
    dialogItems.addView(itemTitle, titleParam);

    final RelativeLayout.LayoutParams bodyParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    bodyParam.addRule(RelativeLayout.ALIGN_LEFT, itemTitle.getId());
    bodyParam.addRule(RelativeLayout.BELOW, itemTitle.getId());
    itemBody.setLayoutParams(bodyParam);
    dialogItems.addView(itemBody, bodyParam);

    dialog.setView(dialogItems);

    dialog.show();
有人知道为什么这样不行吗?问题是,弹出窗口出现了,但是所有的项目都在左上角重叠。谢谢


另外,我已经检查了其他帖子和问题,甚至连答案都不起作用!因此,请修复我的代码,而不是将我链接到另一个问题。

您没有设置ID,因此每个视图都具有相同的ID(-1)。这应该起作用:

private static final int DIALOG_ITEMS_ID = 1;
private static final int ITEM_TITLE_ID = 2;
private static final int ITEM_BODY_ID = 3;
private static final int ICON_ID = 4;

RelativeLayout dialogItems = new RelativeLayout(this);
dialogItems.setId(DIALOG_ITEMS_ID);
EditText itemTitle = new EditText(this);
itemTitle.setId(ITEM_TITLE_ID);
EditText itemBody = new EditText(this);
itemBody.setId(ITEM_BODY_ID);
ImageView dIcon = new ImageView(this);
dIcon.setId(ICON_ID);

您没有设置ID,因此每个视图都具有相同的ID(-1)。这应该起作用:

private static final int DIALOG_ITEMS_ID = 1;
private static final int ITEM_TITLE_ID = 2;
private static final int ITEM_BODY_ID = 3;
private static final int ICON_ID = 4;

RelativeLayout dialogItems = new RelativeLayout(this);
dialogItems.setId(DIALOG_ITEMS_ID);
EditText itemTitle = new EditText(this);
itemTitle.setId(ITEM_TITLE_ID);
EditText itemBody = new EditText(this);
itemBody.setId(ITEM_BODY_ID);
ImageView dIcon = new ImageView(this);
dIcon.setId(ICON_ID);

谢谢,我以为它会自动为这些项目提供自己的id…:Pthanks我假设它会自动为项目提供自己的id…:P