【问题标题】:Position a menu item at the left end of the actionbar将菜单项放置在操作栏的左端
【发布时间】:2017-05-11 01:25:25
【问题描述】:

如何将菜单图标移动到栏的左端,应用名称的左侧,如图所示? 有没有办法只通过xml来做到这一点,而不添加java代码?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
                                                 android:id="@+id/main_content"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:fitsSystemWindows="true"
                                                 tools:context="com.robyn.myapplication.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:background="@drawable/gradient_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

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

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabItem
                android:id="@+id/tab1"
                android:icon="@drawable/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <android.support.design.widget.TabItem
                android:id="@+id/tab2"
                android:icon="@drawable/favo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <android.support.design.widget.TabItem
                android:id="@+id/tab3"
                android:icon="@drawable/hour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email"/>

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

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="com.robyn.myapplication.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_menu_white_24dp"
        android:orderInCategory="0"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"/>
</menu>

【问题讨论】:

  • 只是不要使用菜单并在工具栏中将其作为图像视图

标签: android xml android-actionbar toolbar


【解决方案1】:

根据@Martin De Simone,如果你把图片放在toolbar标签里面,会留下一个小空间,不会在最左边。如果你没问题,你可以像下面这样,

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="left|start">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_dehaze"
            android:contentDescription="@string/logo"/>

    </LinearLayout>

</android.support.v7.widget.Toolbar>

还有更多可能的解决方案。

  1. 这是一种 hack,但如果您的目标是 API 17 级及以上,您可以设置 `layoutDirection="rtl"。 你必须相应地设置你的标题方向
  2. 您必须使用Java 代码来设置toolbar,添加更多行。

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    toolbar.setNavigationIcon(R.drawable.ic_dehaze);  //your icon
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do whatever you want to do here
        }
    });
    

希望对你有帮助

【讨论】:

    【解决方案2】:

    `layoutDirection="rtl" 这对我有帮助

    【讨论】: