【问题标题】:Setting a layout to view.setEnabled(false) does not disable all child views将布局设置为 view.setEnabled(false) 不会禁用所有子视图
【发布时间】:2015-03-10 23:59:55
【问题描述】:

我有一个包含多个嵌套相对布局的布局。在嵌套布局中,我有表单元素,例如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 不会被禁用。我必须进入并将每个子视图单独设置为禁用。安卓的bug?禁用视图是否有最大嵌套限制?我检查了状态,相关布局确实设置为禁用。

【问题讨论】:

  • “这不是错误,而是功能”
  • @KushtrimP。哈哈。一个令人沮丧的功能。

标签: android isenabled


【解决方案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);
}

希望对您有所帮助!

【讨论】:

  • 这对我的工作有何改变?
  • 因为您的代码,没有将视图放入RelativeLayout.LayoutParams。而且你没有setLayoutParams surveyquestion
  • @KristyWelsh 我找到了另一个解决方案,请查看我的答案的 UPDATE
猜你喜欢
  • 2013-11-17
  • 1970-01-01
  • 2021-07-31
  • 2011-08-04
  • 2012-07-03
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多