如何在 Android 中显示和隐藏菜单项?
我有一些关于 android 操作栏和选项卡片段的问题。我有 3 个片段,每当选择第三个片段时,我就会在操作栏上显示菜单项。
我该怎么办?
我尝试在运行时引用该按钮并将可见性设置为 true,但它似乎为空。
I have some questions regarding android action bar and tab fragments. I have 3 fragments and whenever the third fragment is selected, then I will show the menu item on the actiobar.
how should i go about it?
I tried referencing to the button at runtime and set the visibility to true, but it seems to be null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看 Fragment 方法 setHasOptionsMenu(boolean hasMenu)< /a>.这样,每个片段都可以管理自己的菜单项,系统将根据该值处理操作项的隐藏/显示。在您的情况下,您似乎会将第一个和第二个片段设置为 false,而第三个片段将传递 true,因此它会命中 onCreateOptionsMenu 方法。
You should look into the Fragment method setHasOptionsMenu(boolean hasMenu). This way each Fragment can manage its own menu items and the system will take care of the hiding/showing of the action items based on this value. In your case it would seem you would set this to false for your first and second fragments, while the third would pass a true so it hits the onCreateOptionsMenu method.