2013-07-04 74 views
1

我有我的應用程序菜單上的側菜單這樣的代碼:改變菜單項標題從onCreateOptionsMenu

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標題?!

回答

3

你應該能夠做到這一點是這樣的:

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

謝謝it'a工作:) –