2015-06-14 2 views
0

Я пытаюсь использовать пользовательское меню в своем приложении для Android. Я хочу добавить некоторые пункты меню. Для этой цели я добавить следующее в моем 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=".MainActivity"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="always" /> 

    <item 
     android:id="@+id/contact" 
     android:icon="@drawable/ic_star" 
     android:orderInCategory="2000" 
     android:title="@string/Rate" 
     app:showAsAction="always" /> 
</menu> 

И в MainActivity:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     menu.clear(); 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return super.onPrepareOptionsMenu(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,"Hello from settings",Toast.LENGTH_LONG).show(); 
      return true; 

     } 
     if (id == R.id.contact) 
     { 
      startActivity(new Intent(this,ContactUs.class)); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

Но он не работает вообще. Я пробовал некоторые решения на SO, но никто из них не работал.

например. this

Пожалуйста, помогите мне решить эту проблему.

ответ

1

Вы должны установить панель инструментов, как

setActionBar(toolbar); 

в OnCreate()

+0

Спасибо Суреш Джи! Вы спасли мою ночь: D – learner

+0

Счастливое кодирование .. :) –

+0

где определена панель инструментов –

Смежные вопросы