如何在Android工具栏中移除汉堡菜单图标后的大间距?

29

这是一个菜单图标后的大间隙的图片:enter image description here

在暑假期间我的项目停了一个月,回来后发现更新Android Studio后工具栏上出现了比平常更大的间隙。我在SO上找不到任何相关问题/解决方案。如果有人能帮忙,将不胜感激。

我通过以下方式加载汉堡图标(每行代码都放在应用程序中的正确位置,例如类声明,onCreate()等。为了简便起见,我将代码放在这里):

ActionBarDrawerToggle mDrawerToggle;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), dl, toolbar, R.string.nav_open, R.string.nav_closed)
mDrawerToggle.syncState();

这是我的工具栏的xml代码:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways"
    app:title="Test Title">

</android.support.v7.widget.Toolbar>

我尝试过调整各种contentInset的xml属性,但没有一个能够影响到菜单图标后面的间距。

编辑:这个链接展示了我之前使用的东西(可以在页面中间找到)。如果你注意到标题和汉堡图标之间的间距,会发现间距不如这里展示的图片那么宽。就好像间距加倍了一样。


可能是padding-between-actionbars-home-icon-and-title的重复问题。 - Mohamad Damba Putrabangga
以下是针对下面两个答案的补充说明:正如他们所说,它应该放在工具栏布局中。我尝试将其放在主题中,但设置为工具栏布局的主题时无法正常工作。 - vida
2个回答

62

我明白了!我需要在Toolbar布局中设置

app:contentInsetStartWithNavigation="0dp"

输入图像描述


3
这种新的填充方式是在Support Library 24.0.0+中引入的,具体信息请参考以下链接:https://code.google.com/p/android/issues/detail?id=213826 - kefs
@kefs 有趣。不过我不确定为什么,看起来空格太多了。 - David Velasquez
3
谢谢!我们也可以通过编程来实现 toolbar.setContentInsetStartWithNavigation(0); - user25
2
app:contentInsetLeft="0dp" app:contentInsetStart="0dp" app:contentInsetStartWithNavigation="0dp" - Ajit
defaultValue是什么? - user5698345
显示剩余2条评论

16

将以下属性添加到您的工具栏

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

这将禁用Toolbar标题的起始填充


请删除汉堡图标和工具栏之间的填充。@blueware - Ajay Jayendran

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