将布局设置为 view.setEnabled(false) 不会禁用所有子视图

Setting a layout to view.setEnabled(false) does not disable all child views

我有一个包含多个嵌套相对布局的布局。在嵌套布局中,我有表单元素,例如 TextViews、EditTexts 和 Buttons。代码仅针对此示例进行了缩写:

Context con;
LinearLayout survey = new LinearLayout();
RelativeLayout question = new RelativeLayout();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnAnswer = new Button(con);
btnAnswer.setId(GlobalVars.getLatestId());
btnAnswer.addParams(params);
question.addView(btnAnswer);
TextView tvBtnLabel = new TextView(con);
btnAnswer.setId(GlobalVars.getLatestId());
tvBtnLabel.setText("Some Label");

tvBtnLabel.addParams(params);
question.addView(tvBtnLabel);
survey.addView(question);
question.setEnabled(false);
//^^^^does not set child views to disabled state

当我将整个嵌套相对布局设置为 false 时,ButtonTextView 不会被禁用。我必须进入并将每个子视图单独设置为禁用。 android 中的错误?禁用视图是否有最大嵌套限制?我检查了状态,相关布局确实设置为禁用。

试试这个:

Context con;
LinearLayout survey = new LinearLayout(con);
survey.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,  
                                        LayoutParams.MATCH_PARENT);

RelativeLayout question = new RelativeLayout(con);
question.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
                                          LayoutParams.WRAP_CONTENT);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams  
              (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Button btnAnswer = new Button(con);
btnAnswer.setId(GlobalVars.getLatestId());
btnAnswer.addParams(params);

question.addView(btnAnswer);
TextView tvBtnLabel = new TextView(con);
btnAnswer.setId(GlobalVars.getLatestId());
tvBtnLabel.setText("Some Label");

tvBtnLabel.addParams(params);
question.addView(tvBtnLabel);
survey.addView(question);

[更新]

for (int i = 0; i < question.getChildCount(); i++) {
   View child = question.getChildAt(i);
   child.setEnabled(false);
}

希望对您有所帮助!