Android:以编程方式添加视图,并且不遵循布局

时间:2014-03-14 03:27:19

标签: layout relativelayout android-ui

我有onClickListener,其中我尝试将两个视图动态添加到现有ViewGroup。我只是想在另一个的右侧添加一个,但无论我做什么,它们都会在彼此的顶部呈现,左边缘对齐。遵循布局的其他方面。例如,我可以将宽度指定为MATCH_PARENT,并且View将如此呈现。另外,我正在以编程方式模仿我如何为XML中的不同ViewGroup指定布局,并且XML指定的布局正常工作。这是我的代码:

Editable nodeName = nodeSelectView.getText();
View insertPoint = findViewById(R.id.insertionPoint);

//the two views to be added dynamically
EditText nodeView = new EditText(ManageDomainsActivity.this);
Button nodeButton = new Button(ManageDomainsActivity.this);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.LEFT_OF, nodeButton.getId());
nodeView.setLayoutParams(lp);
nodeView.setGravity(Gravity.LEFT);
nodeView.setText(nodeName.toString());

lp = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.RIGHT_OF, nodeView.getId());
lp.addRule(RelativeLayout.ALIGN_BASELINE, nodeView.getId());
lp.addRule(RelativeLayout.ALIGN_BOTTOM, nodeView.getId());
nodeButton.setLayoutParams(lp);
nodeButton.setText("Kill");
nodeButton.setGravity(Gravity.CENTER);

((ViewGroup) insertPoint).addView(nodeView, 0, 
        new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
((ViewGroup) insertPoint).addView(nodeButton, 1, 
        new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));

2 个答案:

答案 0 :(得分:1)

您需要以编程方式设置要创建的视图的ID,因为在没有id的视图上使用getId()会返回NO_ID常量,该常量在RelativeLayout规则中不起作用。以编程方式创建的视图不需要全局唯一ID(仅在视图组中是唯一的),因此您可以将它们设置为1,2,3,...等

答案 1 :(得分:0)

问题是我在addView()中传递的LayoutParameters覆盖了我在相应视图上设置的那些。由于循环依赖性,它也失败了,因为两个视图分别被定义在彼此的右侧和左侧。这对我来说似乎并不循环,它在XML中运行良好,但它在动态布局分配中引发了异常。以下是有效的代码:

Editable nodeName = nodeSelectView.getText();
View insertPoint = findViewById(R.id.insertionPoint);

EditText nodeView = new EditText(ManageDomainsActivity.this);
nodeView.setId(1);
Button nodeButton = new Button(ManageDomainsActivity.this);
nodeButton.setId(2);

RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,   
            RelativeLayout.LayoutParams.WRAP_CONTENT);
lpView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
nodeView.setLayoutParams(lpView);
nodeView.setGravity(Gravity.LEFT);
nodeView.setText(nodeName.toString());

RelativeLayout.LayoutParams lpButton = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);
lpButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lpButton.addRule(RelativeLayout.RIGHT_OF, nodeView.getId());
lpButton.addRule(RelativeLayout.ALIGN_BASELINE, nodeView.getId());
lpButton.addRule(RelativeLayout.ALIGN_BOTTOM, nodeView.getId());
nodeButton.setLayoutParams(lpButton);
nodeButton.setText("Kill");
nodeButton.setGravity(Gravity.CENTER);

((ViewGroup) insertPoint).addView(nodeView, lpView);
((ViewGroup) insertPoint).addView(nodeButton, lpButton);