我已以编程方式将 CheckedTextView 添加到线性布局 View 中。请看下面的代码:
private LinearLayout linearLayout;
private CheckedTextView checkedtextview;
linearLayout = (LinearLayout) findViewById(R.id.statusView);
checkedtextview = new CheckedTextView(ScanStatus.this, null, android.R.attr.listChoiceIndicatorMultiple);
checkedtextview.setText(R.string.applications);
linearLayout.addView(checkedtextview);
稍后在代码中我必须更新checkedtextview,如下所示:
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);
但这会导致崩溃并显示以下日志:
D/AndroidRuntime(24818):关闭虚拟机 E/AndroidRuntime(24818):致命异常:main E/AndroidRuntime(24818):进程:com.example.ashwini.timapp,PID:24818 E/AndroidRuntime(24818): java.lang.IllegalStateException: 指定的子级已经有父级。您必须首先在子级的父级上调用removeView()。
请建议我如何更新 View ?
最佳答案
我认为您首先需要删除 View ,然后更新它。
linearLayout.removeView(checkedtextview);
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);
关于Android CheckedTextView如何更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757393/