如何将工具栏中的关闭按钮向左移动

How to shift the close button in the toolbar to the left

我正在开发这个应用程序。并使用透明的工具栏。我在上面添加了菜单项。

如图

现在如何将工具栏中的关闭按钮移到左侧。

这是代码

MainActivity.java

transparent_toolbar.inflateMenu(R.menu.menu_play_screen);

menu_play_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item

    android:id="@+id/action_back"
    android:orderInCategory="0"
    android:title="@string/action_settings"
    app:showAsAction="always"
    android:icon="@drawable/ic_close_white_24dp"/>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
<item
    android:id="@+id/action_playQueue"
    android:orderInCategory="99"
    android:title="Add to queue"
    app:showAsAction="always"
    android:icon="@drawable/ic_queue_music_white_24dp"
    />

</menu>

menu.xml 做不到。如果您希望按钮位于左侧,您需要为工具栏自定义布局,如下所示:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <Button
            android:id="@+id/close_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|start"
            android:background="?android:selectableItemBackground"
            android:drawableStart="@drawable/close_button"
            android:text="@string/close" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

然后展开工具栏中的其余菜单。

我认为您无法在菜单的帮助下完成此操作。但是你有几个选择:

  1. 使用导航按钮。首先,设置您的自定义 navigation icon, then set the navigation listener to listen for click event.

  2. 工具栏是 ViewGroup,因此您可以向其中添加任何视图。

就我个人而言,我认为第一个选项更符合逻辑,因为 "close" 按钮具有导航功能。此外,您可能希望 set your toolbar as an app bar 将其与您的 fragment/activity.

集成