【问题标题】:How to implement back arrow(Up Button) in the vertical center of the Toolbar如何在工具栏的垂直中心实现后退箭头(向上按钮)
【发布时间】:2015-03-31 22:52:55
【问题描述】:

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

我唯一要做的就是在 android manifest 中:

<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 中包含了工具栏。

我还能做什么?!

【问题讨论】:

  • 你想实现后退箭头(即向上导航)还是让它位于垂直中心? @KiZo
  • 已经实现了,我只想在app_bar的垂直中心
  • 你能发布你的工具栏代码吗?我的意思是xml。
  • 我已经发布了答案。

标签: android android-toolbar


【解决方案1】:

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

如果您将wrap_content 设置为工具栏的高度,那么纵向和横向的高度相同。因此,在纵向模式下,向上按钮保持在中心位置,而在横向模式下,向上按钮向上,底部添加空白空间。

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

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

【讨论】:

  • 它有效,非常感谢你,虽然我不明白为什么我使用 actionBarSize 作为工具栏?
【解决方案2】:
    Toolbar mToolBar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar (mToolBar);
    ActionBar actionBar = getSupportActionBar ();
    actionBar.setElevation (5);
    actionBar.setDisplayHomeAsUpEnabled(true);

【讨论】:

  • 我已经这样做了,但后退按钮仍然不在 app_bar 的垂直中心。后退按钮很好用,但我希望它位于左侧的垂直中心。现在它在左侧,但不在垂直中心。
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多