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

时间:2015-03-08 15:06:23

标签: android isenabled

我有一个包含多个嵌套相对布局的布局。在嵌套布局中,我有表单元素,例如TextView s,EditTextButton s。代码仅为此示例缩写:

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中的错误?是否有禁用视图的最大嵌套限制?我检查了状态,相对布局确实设置为禁用。

1 个答案:

答案 0 :(得分:1)

试试这个:

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);
}

希望这有帮助!