我如何摆脱在新的工具栏与Android SDK API版本21(支持库)额外的填充?

我说的是这张照片上的红色箭头:

这是我正在使用的代码:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:padding="0dp"
        android:layout_margin="0dp">

        <RelativeLayout
            android:id="@+id/action_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:background="#000000">

            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </RelativeLayout>
</Toolbar>

正如你所看到的,我已经将所有相关的填充设置为0,但旋转器周围仍然有填充。我做错了什么,或者我需要做什么来消除额外的填充?

编辑 有些人质疑我为什么要这么做。

根据材料设计规格,转轮应该在左侧72dp处

我需要中和填充谷歌已经放在那里,以适当地放置我的旋转:

编辑2

根据Chris Bane下面的回答,我将contentInsetStart设置为0。对于支持库,你需要使用app命名空间:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="@dimen/action_bar_height"
        android:background="?attr/colorPrimary"
        android:contentInsetStart="0dp"
        android:contentInsetLeft="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v4.widget.DrawerLayout>

当前回答

让你的工具栏像这样:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menuToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@color/white"
android:contentInsetLeft="10dp"
android:contentInsetRight="10dp"
android:contentInsetStart="10dp"
android:minHeight="?attr/actionBarSize"
android:padding="0dp"
app:contentInsetLeft="10dp"
app:contentInsetRight="10dp"
app:contentInsetStart="10dp"></android.support.v7.widget.Toolbar>

你需要加上

contentInset

属性添加空格

请按此链接获取更多- Android提示

其他回答

简单地添加这两行工具栏。然后我们得到新的删除左侧空间bcoz默认为16dp。

android:contentInsetStart="0dp"
app:contentInsetStart="0dp"

以下是我所做的,它在每个版本的Android上都能完美运行。

toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/primary_color"
    app:theme="@style/ThemeOverlay.AppCompat"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp" <!-- Add margin -->
        android:layout_marginStart="16dp"
        android:gravity="left|center"
        android:text="Toolbar Title" <!-- Your title text -->
        android:textColor="@color/white" <!-- Matches default title styles -->
        android:textSize="20sp"
        android:fontFamily="sans-serif-medium"/>

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

java(隐藏默认工具栏标题)

getSupportActionBar().setDisplayShowTitleEnabled(false); // Hide default toolbar title

显示关键字的结果

让你的工具栏像这样:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menuToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@color/white"
android:contentInsetLeft="10dp"
android:contentInsetRight="10dp"
android:contentInsetStart="10dp"
android:minHeight="?attr/actionBarSize"
android:padding="0dp"
app:contentInsetLeft="10dp"
app:contentInsetRight="10dp"
app:contentInsetStart="10dp"></android.support.v7.widget.Toolbar>

你需要加上

contentInset

属性添加空格

请按此链接获取更多- Android提示

((Toolbar)actionBar.getCustomView().getParent()).setContentInsetsAbsolute(0,0);

好的,如果你需要72dp,你不能在xml文件中添加填充的差异吗?这样你可以保持android默认的插入/填充,他们想让我们使用。

所以:72 - 16 = 56

因此:添加56dp的填充,使缩进/边距达到72dp。

或者您可以更改dimension .xml文件中的值。 这就是我现在正在做的。它改变了一切,整个布局,包括工具栏,当以新的正确的Android方式实现时。

Dimen资源文件

我添加的链接显示了2dp的Dimen值,因为我改变了它,但它是默认设置为16dp。仅供参考……