2015-02-06 95 views
1

我已將我的應用程序的主題設置爲NoActionBar並已實施appcompatv7工具欄。 但是,工具欄不顯示menu_main.xml文件中明確提到的項目的任何圖標。 即使默認顯示的溢出圖標也不會出現在工具欄上。Appcompat工具欄不顯示任何圖標

menu_main.xml文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.codesters.materialdesign.MainActivity"> 
    <item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:title="@string/action_settings" 
    app:showAsAction="never" /> 

    <item 
     android:id="@+id/action_navigate" 
     android:orderInCategory="200" 
     android:title="@string/next" 
     app:showAsAction="always" 
     android:icon="@drawable/ic_action_next_item"/> 
</menu> 

styles.xml文件:

<resources> 
    <!-- Base application theme. --> 
    <style name="AppTheme" parent="AppTheme.Base"> 
    </style> 

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

    <style name="CustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light"> 
     <item name="android:textColorPrimary">#FFFF</item> 
     <item name="android:textColorSecondary">#FFFF</item> 
    </style> 
</resources> 

MainActivity文件:

public class MainActivity extends ActionBarActivity { 

    private Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     toolbar = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(toolbar); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT); 
     } 

     if (id == R.id.action_navigate) { 
      Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT); 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+0

我們不能看到你的代碼,除非你顯示你的代碼 – ytRino 2015-02-06 07:59:33

+0

代碼menu_main.xml文件: – themoonraker13 2015-02-06 08:03:06

+0

也活動課? – ytRino 2015-02-06 08:08:46

回答

2

onCreateOptionsMenu(Menu menu)方法和膨脹您在創建的菜單XML。

然後你的代碼應該看起來像。

@Override 
public boolean onCreateOptionsMenu(Menu menu){ 

    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT); 
    } 

    if (id == R.id.action_navigate) { 
     Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT); 
    } 

    return super.onOptionsItemSelected(item); 
} 
+0

錯誤地刪除了重寫的方法。它解決了我的問題。現在一切正常。萬分感謝。 – themoonraker13 2015-02-06 08:23:50

+0

缺少getMenuInflater()。inflate(R.menu.menu_main,menu);是我的問題 – 2015-09-16 13:06:51