2016-06-21 101 views
-1

我是android新手,正在尝试创建自定义工具栏。我想知道是否有任何方法可以在点击设置菜单(3点)时添加选项。在Android工作室的主工具栏的选项菜单中添加选项

enter image description here

enter image description here

+3

的[自定义主工具栏中的Android Studio中的选项菜单(http://stackoverflow.com/questions/可能的复制36442116 /自定义选项菜单的主工具栏在Android工作室) – Ironman

+0

嗨。我确实检查了这个链接。这与我打算做的有点不同。尽管我找到了解决方案。谢谢无论如何.. :) – Mehul

回答

-1

首先,你需要在menu_main.xml添加项(RES>菜单)文件等。

<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="in.amzbizsol.vts.MainActivity"> 
<item 
    android:id="@+id/action_changepassword" 
    android:orderInCategory="1" 
    android:title="Change Password" 
    app:showAsAction="never" /> 
<item 
    android:id="@+id/action_logout" 
    android:orderInCategory="2" 
    android:title="Logout" 
    app:showAsAction="never" /> 

然后在MainActivity创造这样

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@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_changepassword) { 
     Intent intent=new Intent(getApplicationContext(),ChangePassword.class); 
     startActivity(intent); 
     return true; 
    }else if(id==R.id.action_logout){ 
       finish(); 
      } 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
+0

解决方案也是重复的问题http://stackoverflow.com/questions/36442116/customize-the-option-menu-of-main-toolbar-in-android-studio –

+0

严重?? @KapilRajput我花了我的时间自己写这段代码,它看起来很像。我知道你不能欣赏,但至少在指出前阅读答案。 – Akash