您可以通过编辑/创建菜单xml文件添加按钮:
xmlns:app="http://schemas.android.com/apk/res-auto">
android:id="@+id/action_name"
android:icon="@drawable/you_resource_here"
android:title="Text to be seen by user"
app:showAsAction="always"
android:orderInCategory="0"/>
然后在你的活动中,如果你创建了一个新的文件,你需要编辑onCreateOptionsMenu
@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_name) {
return true;
}
return super.onOptionsItemSelected(item);
}