我如何摆脱在新的工具栏与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>

当前回答

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

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.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提示

结合

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

and

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

这对我很管用。

左边的插入是由工具栏的contentInsetStart造成的,默认情况下是16dp。

将其更改为72dp以对齐关键线。

支持库v24.0.0的更新:

为了匹配材质设计规范,有一个额外的属性contentInsetStartWithNavigation,默认为16dp。如果您也有导航图标,请更改此选项。

简单地添加这两行工具栏。然后我们得到新的删除左侧空间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

显示关键字的结果