问题描述
如何使用 Android SDK API 版本 21(支持库)去除新工具栏中的额外填充?
How do I get rid of the extra padding in the new Toolbar with Android SDK API version 21 (the support library)?
我说的是这张图片上的红色箭头:
I am talking about the red arrows on this picture:
这是我使用的代码:
<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,但 Spinner 周围仍有填充.我做错了什么或者我需要做什么来摆脱额外的填充?
As you can see I've set all the relevant padding to 0, but there is still padding around the Spinner. What have I done wrong or what do I need to do to get rid of the extra padding?
编辑有些人质疑我为什么要这样做.
EditSome have questioned why I am trying to do this.
根据 Material Design 规范,微调器应距左侧 72dp
As per the Material Design specs, the spinner should be 72dp from the left side
我需要消除 Google 在那里放置的填充,以便正确放置我的微调器:
I need to neutralize the padding Google have put there in order to properly place my spinner:
编辑 2
根据下面 Chris Bane 的回答,我将 contentInsetStart 设置为 0.对于支持库,您需要使用应用程序命名空间:
As per Chris Bane's answer below I set the contentInsetStart to 0. For the support library you will need to use the app namespace:
<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>
我希望这对某人有所帮助,这让我困惑了好几天.
I hope this helps someone, it had me confused for several days.
推荐答案
左边的插图是由 Toolbar 的 contentInsetStart
默认为 16dp 引起的.
The left inset is caused by Toolbar's contentInsetStart
which by default is 16dp.
将其更改为 72dp 以与关键线对齐.
Change this to 72dp to align to the keyline.
支持库 v24.0.0 的更新:
为了符合 Material Design 规范,有一个附加属性 contentInsetStartWithNavigation
,默认为 16dp.如果您还有导航图标,请更改此设置.
To match the Material Design spec there's an additional attribute contentInsetStartWithNavigation
which by default is 16dp. Change this if you also have a navigation icon.
这篇关于Android API 21 工具栏填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!