我如何摆脱在新的工具栏与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:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

这里的诀窍是使用下面的-

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

然后去掉-

android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"

现在它应该可以在所有设备上正常工作。

其他回答

更新AndroidX工具栏:

<!-- TOOLBAR -->
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetStart="0dp">

    <TextView
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/create_account_title"
        android:textColor="@color/color_dark_grey"/>

</androidx.appcompat.widget.Toolbar>

以防有人在这里绊倒…你也可以设置填充,例如:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

int padding = 200 // padding left and right

toolbar.setPadding(padding, toolbar.getPaddingTop(), padding, toolbar.getPaddingBottom());

或contentInset:

toolbar.setContentInsetsAbsolute(toolbar.getContentInsetLeft(), 200);

结合

android:填充= " 0 dp” 在工具栏的xml中

and

mToolbar。setContentInsetsAbsolute (0, 0) 在代码中

这对我很管用。

以下是我所做的,它在每个版本的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:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

这里的诀窍是使用下面的-

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

然后去掉-

android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"

现在它应该可以在所有设备上正常工作。