如何使LinearLayout高度成为其子组件的最高高度?

时间:2016-04-13 11:13:11

标签: android android-layout

我将两个Checkbox动态添加到Linearlayout。然后在Relativelayout中一个接一个地添加那些Linearlayout。复选框的权重设置为每个占据Linearlayout宽度的50%。现在,如果它们的高度不匹配,则高度较大的复选框的底部会消失。怎么解决这个?这是一个截图:

enter image description here

代码:

LinearLayout ll;
    LinearLayout.LayoutParams lp;
    CheckBox ch;
    int id = 1200, i, j;

    for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) {
        ll = new LinearLayout(NotificationSettings.this);
        lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        ch = new CheckBox(NotificationSettings.this);
        lp.weight = 1.0f;
        ch.setLayoutParams(lp);
        ch.setText(selections.get(i));
        ch.setChecked(isSelected);
        ch.setTextColor(color);
        ch.setId(j);
        ll.addView(ch);
        ch = new CheckBox(NotificationSettings.this);
        ch.setLayoutParams(lp);
        ch.setText(selections.get(i + 1));
        ch.setChecked(isSelected);
        ch.setTextColor(color);
        ch.setId(j + 1);
        ll.addView(ch);
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (id == 1200)
            p.addRule(RelativeLayout.BELOW, addBelow);
        else
            p.addRule(RelativeLayout.BELOW, id);
        ll.setLayoutParams(p);
        ll.setId(++id);
        rl.addView(ll);
    }

修改 当两个复选框都有多行时:

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否可以确保线性布局下方的高度不会太大而无法覆盖上述线性布局?

或者尝试将相对布局参数的高度更改为WRAP_CONTENT

更改

 RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);