GridLayout项目在Nougat中不可见

时间:2018-03-09 09:15:08

标签: android android-6.0-marshmallow grid-layout android-7.0-nougat android-gridlayout

我必须动态构建视图,并且我使用GridLayout作为父布局来连续填充2个视图。代码在Nougat下面的平台上运行得非常好。但是在牛轧糖版本中,视图没有填充。

以下是

的代码部分
private void populateAnswer(List<Answer> answerList, LinearLayout parent) {


    GridLayout gridLayout = new GridLayout(context);
    gridLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
    gridLayout.setAlignmentMode(GridLayout.ALIGN_MARGINS);
    GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

    gridLayout.setLayoutParams(layoutParams);
    gridLayout.setColumnCount(2);


    Answer temp;
    RadioGroup radioGroup = new RadioGroup(context);
    if (answerList != null) {
        for (int i = 0; i < answerList.size(); i++) {
            temp = answerList.get(i);
            if (temp.getaType().equals(RADIO_BUTTON)) {
                RadioButton radioButton = new RadioButton(context);
                radioButton.setTextColor(getResources().getColor(R.color.colorPrimary));
                radioButton.setHighlightColor(getResources().getColor(R.color.colorPrimary));
                GridLayout.LayoutParams rbLayoutParams = new GridLayout.LayoutParams();
                rbLayoutParams.setGravity(Gravity.CENTER);
                rbLayoutParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
                rbLayoutParams.width = GridLayout.LayoutParams.MATCH_PARENT;
                radioButton.setLayoutParams(rbLayoutParams);
                radioButton.setText(temp.getaDesc());
                radioGroup.addView(radioButton);
            } else if (temp.getaType().equals(CHECK_BOX)) {
                CheckBox checkBox = new CheckBox(context);
                checkBox.setTextColor(getResources().getColor(R.color.colorPrimary));
                checkBox.setHighlightColor(getResources().getColor(R.color.colorPrimary));
                checkBox.setText(temp.getaDesc());
                gridLayout.addView(checkBox);
            } else if (temp.getaType().equals(FREE_TEXT)) {
                EditText editText = new EditText(context);
                editText.setHint(temp.getaDesc());
                editText.setHintTextColor(getResources().getColor(android.R.color.darker_gray));
                gridLayout.addView(editText);
            }
        }
    }
    if (radioGroup.getChildCount() > 0) {
        gridLayout.addView(radioGroup);
    }
    parent.addView(gridLayout);

}

我附上了不同设备的屏幕截图。

Nougat ScreenShot Marshmallow ScreenShot

1 个答案:

答案 0 :(得分:0)

使用:

GridLayout.LayoutParams layoutParams = new ViewGroup.LayoutParams();

而不是:

GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();