简书链接:【原创】修改toolbar上弹出菜单checkbox颜色样式
文章字数:152,阅读全文大约需要1分钟

menu.xml

1
2
3
4
5
6
<item
android:checkable="true"
android:id="@+id/action_disable_keyboard"
android:title="@string/disable_keyboard"
app:showAsAction="ifRoom"
></item>

checkable只对toolbar应用有效,否则无法看到checkbox。
根据源代码追踪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

@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);
}

1
2
3
因此得到如下解决办法

新建布局 设置buttontinit

abc_list_menu_item_checkbox.xml

如果使用attr+动态设置主题则需要改成appcompatbox,