這是我的xml代碼
<group
android:id="@ id/group1"
>
<item android:id="@ id/btnNightMode"
android:title="Night Mode"
android:icon="@drawable/night_mode"
android:checkable="true"
android:checked="true"
/>
</group>
這是我的科特林代碼
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (toggle.onOptionsItemSelected(item)) {
return true
}
when (item.itemId) {
R.id.btnNightMode -> {
item.setChecked(false)
if (item.isChecked) {
// If item already checked then unchecked it
item.isChecked = false
toast("la")
} else {
// If item is unchecked then checked it
item.isChecked = true
toast("la")
}
}
}
return super.onOptionsItemSelected(item)
}
由于某種原因,復選框沒有出現在專案旁邊,有什么幫助嗎?它在檔案中說我應該以編程方式設定它,所以我做了,但它仍然沒有顯示復選框
uj5u.com熱心網友回復:
嘗試添加app:actionViewClass="android.widget.CheckBox".
<item
android:id="@ id/btnNightMode"
android:title="Night Mode"
android:icon="@drawable/night_mode"
android:checkable="true"
android:checked="true"
app:actionViewClass="android.widget.CheckBox" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428331.html
