menu.xml
<item
android:checkable="true"
android:id="@+id/action_disable_keyboard"
android:title="@string/disable_keyboard"
app:showAsAction="ifRoom"
></item>
checkable只对toolbar应用有效,否则无法看到checkbox。
根据源代码追踪
<init>:74, ListMenuItemView (androidx.appcompat.view.menu)
newInstance0:-1, Constructor (java.lang.reflect)
newInstance:343, Constructor (java.lang.reflect)
createView:856, LayoutInflater (android.view)
createViewFromTag:1012, LayoutInflater (android.view)
createViewFromTag:963, LayoutInflater (android.view)
inflate:661, LayoutInflater (android.view)
inflate:536, LayoutInflater (android.view)
getView:94, MenuAdapter (androidx.appcompat.view.menu)
measureIndividualMenuWidth:161, MenuPopup (androidx.appcompat.view.menu)
tryShow:174, StandardMenuPopup (androidx.appcompat.view.menu)
show:208, StandardMenuPopup (androidx.appcompat.view.menu)
showPopup:296, MenuPopupHelper (androidx.appcompat.view.menu)
tryShow:182, MenuPopupHelper (androidx.appcompat.view.menu)
run:792, ActionMenuPresenter$OpenOverflowRunnable (androidx.appcompat.widget)
handleCallback:938, Handler (android.os)
dispatchMessage:99, Handler (android.os)
loop:236, Looper (android.os)
main:8063, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:676, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:1011, ZygoteInit (com.android.internal.os)
setCheckable
@Override
public void setCheckable(boolean checkable) {
if (!checkable && mRadioButton == null && mCheckBox == null) {
return;
}
// Depending on whether its exclusive check or not, the checkbox or
// radio button will be the one in use (and the other will be otherCompoundButton)
final CompoundButton compoundButton;
final CompoundButton otherCompoundButton;
if (mItemData.isExclusiveCheckable()) {
if (mRadioButton == null) {
insertRadioButton();
}
compoundButton = mRadioButton;
otherCompoundButton = mCheckBox;
} else {
if (mCheckBox == null) {
insertCheckBox();
}
compoundButton = mCheckBox;
otherCompoundButton = mRadioButton;
}
if (checkable) {
compoundButton.setChecked(mItemData.isChecked());
if (compoundButton.getVisibility() != VISIBLE) {
compoundButton.setVisibility(VISIBLE);
}
// Make sure the other compound button isn't visible
if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) {
otherCompoundButton.setVisibility(GONE);
}
} else {
if (mCheckBox != null) {
mCheckBox.setVisibility(GONE);
}
if (mRadioButton != null) {
mRadioButton.setVisibility(GONE);
}
}
}
最后定位到
private void insertCheckBox() {
LayoutInflater inflater = getInflater();
mCheckBox =
(CheckBox) inflater.inflate(R.layout.abc_list_menu_item_checkbox,
this, false);
addContentView(mCheckBox);
}
因此得到如下解决办法
新建布局 设置buttontinit
abc_list_menu_item_checkbox.xml
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:buttonTint="@color/themeColor"
android:focusable="false"
android:clickable="false"
android:duplicateParentState="true"/>
网友评论