首页 > 解决方案 > 如何删除向上导航箭头和工具栏标题之间​​的填充?

问题描述

在此处输入图像描述

顶部是您设置时向上导航箭头的默认填充

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

我希望工具栏看起来更像底部图像,因为我希望标题指示向上导航箭头将用户发送到的位置而不是对当前活动的描述。到目前为止,我已经尝试将自定义文本视图添加到工具栏,并app:contentInsetStartWithNavigation="0dp"根据这个 类似的问题进行设置。但正如作者所指出的,这不会删除箭头右侧的填充。如何让工具栏看起来更像第二张图片?我的工具栏的当前 xml 如下:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_doc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    app:contentInsetStartWithNavigation="0dp"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="left"
        android:id="@+id/txtTitle"
        android:textColor="#FFFFFF"/>

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

我进一步提供了菜单的 xml,我使用菜单充气器将其添加到工具栏。我漂浮的另一个解决方案是向菜单本身添加箭头图标和文本视图?但是我不确定如何为新箭头图标提供相同的向上导航行为。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_baseline_home_24px"
        app:showAsAction="always"/>
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

标签: androidxmlandroid-layouttoolbarandroid-toolbar

解决方案


尝试设置app:contentInsetStart0dp而不是app:contentInsetStartWithNavigation.

    <android.support.v7.widget.Toolbar
        ...
        app:contentInsetStart="0dp">

推荐阅读