是否以编程方式将RelativeLayout中的两个配对TextView与多行TextView重叠?

时间:2018-01-31 16:57:10

标签: android android-layout android-relativelayout

我设法通过添加相对布局的to_end_of来实现这一点,但是长文本似乎也有问题 enter image description here

这是执行此操作的代码:

        RelativeLayout.LayoutParams keyParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        keyParams.addRule(RelativeLayout.ALIGN_PARENT_END);

        RelativeLayout.LayoutParams valueParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        valueParams.addRule(RelativeLayout.ALIGN_PARENT_START);



        for (Map.Entry<String, String> item : model.getCompanyProperties().entrySet()) {
            RelativeLayout layout = new RelativeLayout(this);
            TextView keyText = new TextView(this);
            keyText.setLayoutParams(keyParams);
            keyText.setText(item.getKey());
            keyText.setSingleLine(false);


            TextView valueText = new TextView(this);
            valueText.setGravity(Gravity.START);
            valueText.setTextSize(18);
            valueText.setText(item.getValue());
            valueParams.addRule(RelativeLayout.START_OF, keyText.getId());
            keyParams.addRule(RelativeLayout.END_OF, valueText.getId());
            valueText.setLayoutParams(valueParams);


            layout.addView(keyText);
            layout.addView(valueText);
            repoPropertiesLayout.addView(layout,
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        }

    }

1 个答案:

答案 0 :(得分:0)

最后,我设法通过设置权重来查看和创建LinearLayout来克服这种情况,这里是代码:

 LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.END);
        TextView valueText = new TextView(this);
        valueText.setText(item.getValue());
        layout.addView(valueText);
        TextView keyText = new TextView(this);
        keyText.setText(item.getKey());
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                0,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                1.0f
        );
        valueText.setLayoutParams(param);
        keyText.setLayoutParams(param);
        layout.addView(keyText);