如何将单个菜单项添加到android中的工具栏?

Lej*_* KR 12 android toolbar menuitem android-actionbaractivity android-toolbar

我只是想在工具栏的左侧有一个后退按钮.但是当我添加下面的代码时,会出现在toolbar的右侧.我可以将它更改为左侧吗?

我的代码

<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.me.myapp.activities.Timer">
    <item
        android:id="@+id/backButton"
        android:title="Back Button"
        android:icon="@mipmap/back_icon"
        app:showAsAction="ifRoom"></item>
</menu>
Run Code Online (Sandbox Code Playgroud)

Vik*_*tel 26

您只需要Back在ToolBar左上角的图标然后进行配置Toolbar.

mToolBar = (Toolbar) findViewById(R.id.toolbarLayout);
mToolBar.setTitle("Toolbar");
mToolBar.setNavigationIcon(R.drawable.ic_back_shadow);
setSupportActionBar(mToolBar);
Run Code Online (Sandbox Code Playgroud)

由于ToolBar菜单项是完全取决于您的装置已是RTL产品主要用于(从右到左)的支持与否menu items,而不是对back key.

此外,你可以处理后面的图标

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
Run Code Online (Sandbox Code Playgroud)