简体   繁体   English

相对布局无法以编程方式添加

[英]Relative layout not working programmatically added

i have just started android programming, wrote a quick code, and havn't managed to get it to do what i want.我刚刚开始 android 编程,写了一个快速代码,但没有设法让它做我想做的事。 Basically, i want a dialog box to appear, with 2 text boxes and an image shown in a particular layout.基本上,我希望出现一个对话框,其中包含 2 个文本框和一个以特定布局显示的图像。 I have the following code:我有以下代码:

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();

Does anyone know why this wouldn't be working?有谁知道为什么这不起作用? The problem is, the popup appears, but all the items are just overlapping in the top left.问题是,弹出窗口出现了,但所有项目都只是在左上角重叠。 Thanks谢谢

PS i have checked other posts and questions, and even the answers don't work. PS我检查了其他帖子和问题,甚至答案都不起作用。 So please just fix my code rather than linking me to another question.因此,请修复我的代码,而不是将我链接到另一个问题。

You didn't set the IDs so every view has the same ID (-1).您没有设置 ID,因此每个视图都具有相同的 ID (-1)。 This should work:这应该有效:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM