android – 如何通过id获取MenuItem

android – 如何通过id获取MenuItem,第1张

概述我的res / menu / student_marks.xml文件中有menuItem: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".StudentMarks" > 我的res / menu / student_marks.xml文件中有menuItem:
<menu xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    tools:context=".StudentMarks" >    <item androID:ID="@+ID/action_selected_year"        androID:title="@string/action_selected_year"        androID:showAsAction="withText|ifRoom"         /></menu>

现在我需要这个项目来设置我的应用程序的特定部分的标题.
我可以使用此方法中的特定项目:

onoptionsItemSelected(MenuItem item)

问题是我需要项目’action_selected_year’而不使用此方法,但在我的程序的另一部分.我不知道如何得到它.

解决方法
Menu optionsMenu;    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {       getMenuInflater().inflate(R.menu.main,menu);       //  store the menu to var when creating options menu       optionsMenu = menu;    }

示例:更改第一个menuItem上的图标(optionsMenu应为!= null)

optionsMenu.getItem(0).setIcon(getResources()    .getDrawable(R.drawable.ic_action_green));
总结

以上是内存溢出为你收集整理的android – 如何通过id获取MenuItem全部内容,希望文章能够帮你解决android – 如何通过id获取MenuItem所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1137462.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存