我已經編程添加CheckedTextView到linearlayout視圖。請看下面的代碼:Android CheckedTextView如何更新視圖
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 ):關閉VM E/AndroidRuntime(24818):致命異常:主要 E/AndroidRuntime(24818):進程:com.example.ashwini.timapp,PID:24818 E/AndroidRuntime(24818):java.lang。 IllegalStateE xception:指定的孩子已經擁有父母。您必須先調用子對象的父對象的removeView()。
請問我該如何更新視圖?
讓我知道如果第一個變體不起作用。也許你還需要調用'checkedtextview.invalidate()' –
我是初學者到android。如何引用checkedtextview?你能解釋一下代碼片段嗎? – ashkus
你已經擁有它了,因爲我看到了。這是你的班級裏的私人變量。一旦你在開始創建它 - 你有一個參考,並不需要每次更新時重新創建它。您可以在運行時在類中更改此視圖的屬性,也可以調用'invalidate()'方法在屏幕上重繪它 –