从onCreateOptionsMenu更改MenuItem标题

时间:2013-07-04 00:44:31

标签: android menu menuitem

我在侧边菜单中的应用菜单上有这样的代码:

main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/menu" 
        android:showAsAction="always"
        android:title="@string/menu">
        <menu>
            <item android:id="@+id/edit" 
        android:title="@string/edit" 
        android:icon="@android:drawable/ic_menu_edit"/>

        </menu>
    </item>
</menu>

MainActive.java

boolean isEdit=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        isEdit=sp.getBoolean("isEdit", false);
    }
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        android.view.MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
         if(isEdit)
            // change R.id.edit title to "test1"
        return true;
    }

如何从if(isEdit)条件更改R.id.edit标题?!

1 个答案:

答案 0 :(得分:4)

你应该能够这样做:

menu.findItem(R.id.menuitemname).setTitle(R.string.foo);