我的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"
style="@style/AppTheme" />
</menu>
Run Code Online (Sandbox Code Playgroud)
现在我需要这个项目来设置我的应用程序的特定部分的标题.
我可以使用此方法中的特定项目:
onOptionsItemSelected(MenuItem item)
Run Code Online (Sandbox Code Playgroud)
问题是我需要项目'action_selected_year'而不使用此方法,但在我的程序的另一部分.
我不知道如何得到它.
小智 21
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;
}
Run Code Online (Sandbox Code Playgroud)
并获得一个菜单项:
MenuItem item = optionsMenu.findItem(R.id. action_selected_year);
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
示例:更改第一个menuItem上的图标(optionsMenu应为!= null)
optionsMenu.getItem(0).setIcon(getResources()
.getDrawable(R.drawable.ic_action_green));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11833 次 |
| 最近记录: |