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

49

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

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

返回图标和工具栏之间的间距

工具栏的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" />

我尝试使用以下代码来解决这个问题,但是没有任何改变发生。

@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);
}

有没有解决这个问题的方法?


你可以创建自定义操作栏并按照你的喜好显示项目。 - JuLes
1
为什么这是个问题?这是平台开发人员的设计决策,也是谷歌 Android 应用程序的外观。 - Egor
@egor 有时候标题会更长,如果我们能显示2/3个字母,它会更美观、更有意义。此外,在小事情上进行实验也很有趣。 - Sagar Chapagain
我同意@Egor的观点。 - Tushar Monirul
如果您保持标题简短,而不是调整默认系统UI,则可以获得更好的用户体验。然而,我同意每个应用程序都是独特的这一观点:与您的用户一起测试您的UI,如果他们对此类调整有良好反应并发现更长的标题有用,则这将是正确的方法。 - Egor
5个回答

109

添加

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"

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" />

@Sagar Chapagain 很高兴知道这个消息。 - Harshad Pansuriya
1
这是一个不错的解决方案,非常有帮助。 - Praveen Mishra
1
仍然存在间隙!@Sagar Chapagain - Ajay Jayendran
1
在我的情况下,我没有使用这样的工具栏,我该怎么办?有没有任何方法可以通过编程来改变它。 - Vijay

10

使用 MaterialToolbarandroidx.appcompat.widget.Toolbar 可以使用以下属性:

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

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

  • titleMarginStart: 指定工具栏标题起始侧的额外空间。如果同时指定此属性和 titleMargin 属性,则该属性优先。边距值应为正数。

只需在您的布局中使用:

    <com.google.android.material.appbar.MaterialToolbar
        app:contentInsetStartWithNavigation="0dp"
        app:titleMarginStart="0dp"
        ..>

默认情况下:
在此输入图片描述

使用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>

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

1
请添加这行代码: app:contentInsetStartWithNavigation="0dp"
 <android.support.v7.widget.Toolbar
                android:id="@+id/share"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:navigationIcon="@drawable/action_back"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@{title}"
                android:background="4855b5"
                app:titleTextColor="ffffff"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                app:titleTextAppearance="@style/Toolbar.TitleText"
                app:contentInsetStartWithNavigation="0dp" />

0
在Flutter Android中使用titleSpacing: 0,来设置**应用栏**的标题间距。
**示例代码**
appBar: AppBar(
    actions: <Widget>[
      Icon(Icons.search_rounded),
      Icon(Icons.notifications),
      Icon(Icons.add_shopping_cart),
    ],
    leadingWidth: 26,
    titleSpacing: 0,
    backgroundColor: Colors.white70,
    leading: Icon(Icons.menu),
    elevation: 100.0,
    iconTheme: IconThemeData(color: Colors.black),
    title: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        appbarIcon,
      ],
    ),
  )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接