如何缩小导航图标和工具栏标题之间​​的差距?

Sag*_*ain 28 android text android-layout android-support-library android-toolbar

我的问题是导航抽屉图标和工具栏标题之间​​的额外空间.示例图片如下:

导航抽屉图标和标题之间的间距

后退图标和工具栏之间的间距

工具栏的xml视图是

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
Run Code Online (Sandbox Code Playgroud)

我试图通过使用下面的代码解决这个问题,但没有发生任何变化.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //toolbar.setTitleMarginStart(0);
    toolbar.setTitleMarginStart(-8);
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以解决这个问题吗?

Har*_*iya 66

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
Run Code Online (Sandbox Code Playgroud)

到了ToolBar.

完整代码:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp" />
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 11

通过MaterialToolbar和 theandroidx.appcompat.widget.Toolbar您可以使用这些属性:

  • contentInsetStartWithNavigation:当存在导航按钮(例如向上按钮)时,栏中内容视图的最小插入量(默认值= 72dp)。

  • contentInsetStart:栏内内容视图的最小插入量。导航按钮和菜单视图除外(默认值 = 16dp

  • titleMarginStart:指定工具栏标题开始侧的额外空间。如果同时指定了此属性titleMargin,则此属性优先。保证金值应该是正值。

只需在您的布局中使用:

    <com.google.android.material.appbar.MaterialToolbar
        app:contentInsetStartWithNavigation="0dp"
        app:titleMarginStart="0dp"
        ..>
Run Code Online (Sandbox Code Playgroud)

默认:
在此输入图像描述

app:contentInsetStartWithNavigation="0dp"
在此输入图像描述

app:contentInsetStartWithNavigation="0dp"app:titleMarginStart="0dp"
在此输入图像描述

您还可以定义自定义样式:

<style name="MyToolbar" parent="....">
    <item name="titleMarginStart">0dp</item>
    <item name="contentInsetStart">..dp</item>
    <item name="contentInsetStartWithNavigation">..dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)


naw*_*aab 5

app:contentInsetStartWithNavigation="0dp"在工具栏中添加