[英]Show and hide menu icon in toolbar with Fragment in Android Studio
I am trying to use this code to show and hide menu icon in toolbar with Fragment in Android Studio:我正在尝试使用此代码在 Android Studio 中使用片段显示和隐藏工具栏中的菜单图标:
@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);
}
}
but the icon is not shown even though the value of the flag statusBlueTooth = true, but just when I touch the toolbar the icon is shown, what do I need to do so that this is shown according to the value of flag statusBlueTooth但是即使标志statusBlueTooth = true的值也不会显示图标,但是当我触摸工具栏时会显示图标,我需要做什么才能根据标志statusBlueTooth的值显示
Try adding this after in your onCreate of your Fragment:尝试在你的片段的 onCreate 之后添加这个:
setHasOptionsMenu(true);
and check IconBTON != null
before setting the visibility.并在设置可见性之前检查
IconBTON != null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.