android - 如何使工具栏后退按钮居中?

标签 android material-design android-toolbar android-actionbaractivity

我在尝试将支持工具栏上的后退按钮居中时遇到问题。 我在 ActionBarActivity 中使用它。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:toolbar="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    toolbar:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    toolbar:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

并在 Activity 的 onCreate() 中设置向上导航,如下所示:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setTitle(R.string.title_activity_scanner);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

但是,我得到的是:

如您所见,后退按钮放错了位置

编辑: 问题似乎在于 ?attr/actionBarSize 的自定义值设置为 40dp,然而,现在事实证明,它是放错位置的标题。

最佳答案

来自 developer.android.com :

Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:

A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. The navigation button is vertically aligned within the Toolbar's minimum height, if set.

因此,如果您将 minHeight 属性设置为与工具栏高度(layout_height )相同,则后退箭头将垂直居中。

关于android - 如何使工具栏后退按钮居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29890861/

相关文章:

android - 喷气背包中 +(...)% 颜色的表面组成

css - Angular Material 设计 css 文件中缺少 "md-accent"css 类

android - 状态栏下滚动的工具栏

android - 如何避免带有 Jetpack 导航和底部选项卡的向上按钮?

android-saripaar v2 messageResId 必须是常量

android - Robolectric 和 chrisbanes/ActionBar-PullToRefresh Activity 测试

java - 有没有任何预定义的方法可以通过在 Android 中提供整数来获取字母表?

android - 在 goBack() 之后从 WebView 历史中获取 Html 页面的标题?

reactjs - 使用 React、React Redux 或 React Context 和 Material UI 的多语言应用程序的架构示例

java - 工具栏不会折叠,因为它应该在滚动期间使用 TabLayout