如何在工具栏的垂直中心实现后退箭头(向上按钮)

How to implement back arrow(Up Button) in the vertical center of the Toolbar

在横向模式的工具栏中添加后退按钮时,后退按钮不在工具栏的垂直中心。

我唯一做的就是在 android 清单中:

<activity
        android:name="tack.hardcode.com.tack.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape">

这是我的工具栏代码

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3F51B5">

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

我还实现了 v21.styles,并且在我的 mainActivity 中包含了工具栏。

我还应该做什么?!

    Toolbar mToolBar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar (mToolBar);
    ActionBar actionBar = getSupportActionBar ();
    actionBar.setElevation (5);
    actionBar.setDisplayHomeAsUpEnabled(true);

您将工具栏高度设置为 wrap_content 会导致此问题。 Actionbar 纵向高度始终为 56dp,横向高度始终为 48dp。 (对于移动设备)

如果您将 wrap_content 设置为工具栏的高度,则纵向和横向的高度相同。因此,在纵向模式下,向上按钮保持在中间,而在横向模式下它会上升并在底部添加空 space。

因此,将工具栏高度更改为 android:layout_height="?actionBarSize"。 通过更改此设置,工具栏在纵向和横向模式下都采用与 actionbar 相同的高度。

希望对您有所帮助。我希望现在你清楚了。