【问题标题】:GridLayout Items not visible in NougatGridLayout 项目在 Nougat 中不可见
【发布时间】:2018-03-09 09:15:07
【问题描述】:

我必须动态构建视图,并且我使用 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);

}

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

【问题讨论】:

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


    【解决方案1】:

    使用:

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

    代替:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 2014-11-07
      • 1970-01-01
      相关资源
      最近更新 更多