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

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

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

支持库v24.0.0的更新:

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


上面的答案是正确的,但仍然有一件事可能会产生问题(至少它确实给我带来了一个问题)

我使用了以下,它不能正常工作在旧设备上-

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

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

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

然后去掉-

android:contentInsetStart="0dp"
android:contentInsetLeft="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 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);

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

所以:72 - 16 = 56

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

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

Dimen资源文件

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


结合

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

and

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

这对我很管用。


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

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

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

更新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>