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

时间:2016-11-21 10:15:05

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

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

spacing between nav-drawer icon and title

spacing between back icon and 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" />

我尝试使用以下代码解决此问题,但未发生任何更改。

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

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

5 个答案:

答案 0 :(得分:55)

添加

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

答案 1 :(得分:3)

通过MaterialToolbarandroidx.appcompat.widget.Toolbar,您可以使用以下属性:

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

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

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

只需在您的布局中使用:

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

默认值:
enter image description here

使用app:contentInsetStartWithNavigation="0dp"
enter image description here

使用app:contentInsetStartWithNavigation="0dp"app:titleMarginStart="0dp"
enter image description here

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

<style name="MyToolbar" parent="....">
    <item name="titleMarginStart">0dp</item>
    <item name="contentInsetStart">..dp</item>
    <item name="contentInsetStartWithNavigation">..dp</item>
</style>

答案 2 :(得分:2)

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

答案 3 :(得分:0)

Add this line 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" />

答案 4 :(得分: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,
      ],
    ),
  )