在 Android Studio 中使用片段显示和隐藏工具栏中的菜单图标
Show and hide menu icon in toolbar with Fragment in Android Studio
我正在尝试使用此代码在 Android Studio 中使用 Fragment 显示和隐藏工具栏中的菜单图标:
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem IconBTON = menu.findItem(R.id.myIcon);
if (statusBlueTooth == true){
IconBTON.setVisible(true);
}
if (statusBlueTooth == false){
IconBTON.setVisible(false);
}
}
但是即使标志 statusBlueTooth = true 的值也没有显示图标,但是当我触摸工具栏时图标显示了,我需要做什么才能根据值显示标记状态蓝牙
尝试在片段的 onCreate 之后添加:
setHasOptionsMenu(true);
并在设置可见性之前检查 IconBTON != null
。
我正在尝试使用此代码在 Android Studio 中使用 Fragment 显示和隐藏工具栏中的菜单图标:
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem IconBTON = menu.findItem(R.id.myIcon);
if (statusBlueTooth == true){
IconBTON.setVisible(true);
}
if (statusBlueTooth == false){
IconBTON.setVisible(false);
}
}
但是即使标志 statusBlueTooth = true 的值也没有显示图标,但是当我触摸工具栏时图标显示了,我需要做什么才能根据值显示标记状态蓝牙
尝试在片段的 onCreate 之后添加:
setHasOptionsMenu(true);
并在设置可见性之前检查 IconBTON != null
。