【问题标题】:How to reduce the gap between navigation icon and toolbar title?如何缩小导航图标和工具栏标题之间​​的差距?
【发布时间】:2017-04-04 16:49:56
【问题描述】:

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

工具栏的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);
}

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

【问题讨论】:

  • 您可以创建自定义操作栏,并根据需要显示项目。
  • 为什么会出现这个问题?这是平台开发人员的设计决定,这也是 Google 的 Android 应用的外观。
  • @egor 有时标题会更长,如果我们可以多显示 2/3 个字母,它可以提供更好的外观和含义。另外,在小事上做实验也很有趣。
  • 我同意@Egor
  • 我想说,如果你保持标题简短,而不是调整默认系统 UI,你会得到更好的用户体验。不过,我同意每个应用都是独一无二的观点:与用户一起测试你的 UI,如果他们对这种调整反应良好并发现更长的标题有用 - 那么这就是要走的路。

标签: android android-layout text android-support-library android-toolbar


【解决方案1】:

添加

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 很高兴知道这一点。
  • 这是一个不错的解决方案。真的很有帮助。
  • stackoverflow.com/questions/47207491/… ..我仍然有差距。! @Sagar Chapagain
  • 在我的情况下,我没有使用这样的工具栏,我该怎么办?有没有办法以编程方式进行更改。
【解决方案2】:

通过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>

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      添加这一行 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" />
      

      【讨论】:

        【解决方案5】:

        在 Flutter Android 中使用 titleSpacing: 0, 在 ** App Bar** 布局中

        ** 示例代码 **

        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,
              ],
            ),
          )
        

        【讨论】: