【问题标题】:How to remove padding between upnavigation arrow and Toolbar Title?如何删除向上导航箭头和工具栏标题之间​​的填充?
【发布时间】:2018-08-08 07:10:48
【问题描述】:

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

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

我希望工具栏看起来更像底部图像,因为我希望标题指示向上导航箭头会将用户发送到 的位置,而不是对当前活动的描述。到目前为止,我已尝试将自定义文本视图添加到工具栏,并根据 this 类似问题设置 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>

【问题讨论】:

    标签: android xml android-layout toolbar android-toolbar


    【解决方案1】:

    尝试将app:contentInsetStart 设置为0dp 而不是app:contentInsetStartWithNavigation

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

    【讨论】:

    • 这会产生与app:contentInsetStartWithNavigation="0dp" 相同的间距。我也试过设置app:contentInsetLeft="0dp"