Android API 21 工具栏填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26455027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android API 21 Toolbar Padding
提问by James Cross
How do I get rid of the extra padding in the new Toolbar with Android SDK API version 21 (the support library)?
如何使用 Android SDK API 版本 21(支持库)去除新工具栏中的额外填充?
I am talking about the red arrows on this picture:
我说的是这张图片上的红色箭头:
Here is the code I am using:
这是我正在使用的代码:
<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>
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?
如您所见,我已将所有相关填充设置为 0,但 Spinner 周围仍有填充。我做错了什么或者我需要做什么来摆脱额外的填充?
EditSome have questioned why I am trying to do this.
编辑有些人质疑我为什么要这样做。
As per the Material Design specs, the spinner should be 72dp from the left side
根据 Material Design 规范,微调器应该距离左侧 72dp
I need to neutralize the padding Google have put there in order to properly place my spinner:
我需要中和谷歌放置在那里的填充,以便正确放置我的微调器:
Edit 2
编辑 2
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:
根据下面 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>
I hope this helps someone, it had me confused for several days.
我希望这对某人有所帮助,这让我困惑了好几天。
回答by Chris Banes
The left inset is caused by Toolbar's contentInsetStart
which by default is 16dp.
左边的插图是由工具栏引起的contentInsetStart
,默认情况下是 16dp。
Change this to 72dp to align to the keyline.
将其更改为 72dp 以与关键线对齐。
Update for support library v24.0.0:
支持库 v24.0.0 的更新:
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.
为了符合 Material Design 规范,还有一个contentInsetStartWithNavigation
默认为 16dp的附加属性。如果您还有导航图标,请更改此设置。
回答by Varundroid
Above answer is correct but there is still one thing that might create issues (At least it did create an issue for me)
上面的答案是正确的,但仍有一件事可能会产生问题(至少它确实为我带来了问题)
I used the following and it doesn't work properly on older devices -
我使用了以下内容,但在旧设备上无法正常工作 -
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
The trick is here just use the following -
诀窍是在这里只需使用以下内容 -
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
and get rid of -
并摆脱——
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
And now it should work fine throughout all the devices.
现在它应该可以在所有设备上正常工作。
Hope it helps.
希望能帮助到你。
回答by Amit Walke
Simpley add this two line in toolbar. Then we get new removed left side space bcoz by default it 16dp.
Simpley 在工具栏中添加这两行。然后我们得到新的移除的左侧空间 bcoz,默认为 16dp。
android:contentInsetStart="0dp"
app:contentInsetStart="0dp"
回答by worked
In case someone else stumbles here... you can set paddingas well, for instance:
如果其他人在这里绊倒……您也可以设置填充,例如:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
int padding = 200 // padding left and right
toolbar.setPadding(padding, toolbar.getPaddingTop(), padding, toolbar.getPaddingBottom());
Or contentInset:
或contentInset:
toolbar.setContentInsetsAbsolute(toolbar.getContentInsetLeft(), 200);
回答by Jraco11
A combination of
组合
android:padding="0dp"
In the xml for the Toolbar
android:padding="0dp"
在工具栏的 xml 中
and
和
mToolbar.setContentInsetsAbsolute(0, 0)
In the code
mToolbar.setContentInsetsAbsolute(0, 0)
在代码中
This worked for me.
这对我有用。
回答by Michael
Here is what I did and it works perfectly on every version of Android.
这是我所做的,它在每个版本的 Android 上都能完美运行。
toolbar.xml
工具栏.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>
MyActivity.java(To hide default toolbar title)
MyActivity.java (隐藏默认工具栏标题)
getSupportActionBar().setDisplayShowTitleEnabled(false); // Hide default toolbar title
Result with Keylines Shown
显示关键线的结果
回答by Aneh Thakur
Make your toolbar like:
让你的工具栏像:
<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>
You need to add
你需要添加
contentInset
内容插入
attribute to add spacing
添加间距的属性
please follow this link for more - Android Tips
请点击此链接了解更多信息 - Android 提示
回答by Someone Somewhere
this works for me on my Android 7.11 phone:
这适用于我的 Android 7.11 手机:
<!-- TOOLBAR -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetStartWithNavigation="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"/>
</android.support.v7.widget.Toolbar>
note:I had absolutely no success with padding=0, or contentInsetLeft=0, or contentInsetStart=0
注意:我在 padding=0、contentInsetLeft=0 或 contentInsetStart=0 时绝对没有成功
回答by JamisonMan111
Ok so if you need 72dp, couldn't you just add the difference in padding in the xml file? This way you keep Androids default Inset/Padding that they want us to use.
好的,如果您需要 72dp,您不能在 xml 文件中添加填充差异吗?通过这种方式,您可以保留他们希望我们使用的 Android 的默认插入/填充。
So: 72-16=56
所以:72-16=56
Therefor: add 56dp padding to put yourself at an indent/margin total of 72dp.
因此:添加 56dp 的内边距使您自己的缩进/边距总计为 72dp。
Or you could just change the values in the Dimen.xml files. that's what I am doing now. It changes everything, the entire layout, including the ToolBar when implemented in the new proper Android way.
或者您可以更改 Dimen.xml 文件中的值。这就是我现在正在做的事情。当以新的适当的 Android 方式实现时,它会改变一切,整个布局,包括 ToolBar。
The link I added shows the Dimen values at 2dp because I changed it but it was default set at 16dp. Just FYI...
我添加的链接以 2dp 显示 Dimen 值,因为我更改了它,但默认设置为 16dp。仅供参考...
回答by Ahmet Zorer
((Toolbar)actionBar.getCustomView().getParent()).setContentInsetsAbsolute(0,0);