在 Android 中使用负边距是一种不好的做法吗? [英] Is it a bad practice to use negative margins in Android?

查看:33
本文介绍了在 Android 中使用负边距是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过为其中一个视图设置负边距来重叠视图,使其侵入另一个视图的边界框.

Overlapping views by setting a negative margin to one of them so that it invades the bounding box of another view.

如果应该重叠布局,它似乎以您期望的方式工作.但我不想因为在不知不觉中做错事情而遇到更大的问题.模拟器、物理设备,当你使用负边距时,一切似乎都正常工作,一个视图侵入另一个视图的边界框,并且取决于它在布局中的声明方式,它将位于另一个视图的上方或下方.

It seems to work the way you'd expect with overlapping of the layouts if they should. But I don't want to run into a bigger problem for unknowingly not doing things right. Emulators, physical devices, you name it, when you use negative margins everything seems to work correctly, one view invades another's views bounding box and depending on how it's declared in the layout it will be above or below the other view.

我也知道从 API 21 开始我们可以设置 translationZelevation 属性使视图出现在其他视图的上方或下方,但 我的担忧 基本上来自 在文档中为 layout_margin 属性明确指出边距值应该是正的,让我引用一下:

I'm also aware that since API 21 we can set the translationZ and elevation attributes to make view appear above or below other views but my concern basically comes from the fact that in the documentation for the layout_margin attributes it's clearly specified that margin values should be positive, let me quote:

摘录:
在此视图的左侧、顶部、右侧和底部指定额外空间.这个空间在这个视图的边界之外.边距值应为正.必须是一个维度值,它是一个浮点数,附加一个单位,例如14.5sp".可用单位有:px(像素)、dp(与密度无关的像素)、sp(基于首选字体大小的缩放像素)、in(英寸)、mm(毫米)...

Excerpt:
Specifies extra space on the left, top, right and bottom sides of this view. This space is outside this view's bounds. Margin values should be positive. Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)...

自从最初问这个问题以来的几年里,我没有遇到任何负边距的问题,并尝试尽可能避免使用它们,但是没有遇到任何问题,所以即使文档指出,我并不太担心.

In the years since originally asking this question I haven't had any issues with negative margins, did try to avoid using them as much as possible, but did not encounter any issues, so even though the documentation states that, I'm not too worried about it.

推荐答案

2010 年,@RomainGuy(Android 核心工程师)表示 负边距具有未指定的行为.

In 2010, @RomainGuy (core Android engineer) stated that negative margins had unspecified behavior.

2011 年,@RomainGuy 表示您可以在 LinearLayoutRelativeLayout<上使用负边距/code>.

In 2011, @RomainGuy stated that you can use negative margins on LinearLayout and RelativeLayout.

在 2016 年,@RomainGuy 表示 它们从未得到官方支持,也不会受到 约束布局.

In 2016, @RomainGuy stated that they have never been officially supported and won't be supported by ConstraintLayout.

不过,解决这个限制很容易.

It is easy to work around this limitation though.

在基本视图的底部添加一个辅助视图(高度为 0dp,宽度限制为父视图),在底部添加您想要的边距.
然后将您的视图放置在此视图下方,有效地允许它具有负"边距,但不必使用任何不受支持的负值.

Add an helper view (height 0dp, width constrained to parent) at the bottom of your base view, at the bottom add the margin you want.
Then position your view below this one, effectively allowing it to have a "negative" margin but without having to use any unsupported negative value.

这篇关于在 Android 中使用负边距是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆