Android 用于叠加层的 FrameLayout 与 RelativeLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22875453/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:31:03  来源:igfitidea点击:

FrameLayout vs RelativeLayout for overlays

androidandroid-relativelayoutandroid-framelayout

提问by Jaguar

I need to implement an overlay (translucent) screen for my app, something similar to Showcase View

我需要为我的应用程序实现一个覆盖(半透明)屏幕,类似于Showcase View

My guess was to use FrameLayoutfor this usecase, because it is used to stack items on top of each other. But I was surprised to see that the above library uses RelativeLayout.

我的猜测是FrameLayout用于这个用例,因为它用于将项目堆叠在一起。但是我很惊讶地看到上面的库使用RelativeLayout.

My question is when to use FrameLayoutthen, if not in cases like this? What are the disadvantages if I go the FrameLayoutway?

我的问题是什么时候使用FrameLayout,如果不是在这种情况下?如果我走这FrameLayout条路有什么坏处?

采纳答案by scottt

A common rule of thumb when choosing layouts is to select the combination that results in the smallest number of nested layout views.

选择布局时的一个常见经验法则是选择导致嵌套布局视图数量最少的组合。

Specific to your question, RelativeLayout is larger and more capable than the much simpler FrameLayout. So for simple layouts, the latter is probably more efficient. But if using RelativeLayout and it's added positioning options allows you to implement your GUI in a smaller number of layout views, then that would likely be a better choice.

具体到您的问题,RelativeLayout 比简单得多的 FrameLayout 更大,功能更强大。所以对于简单的布局,后者可能更有效。但是如果使用 RelativeLayout 并且它添加的定位选项允许您在较少数量的布局视图中实现您的 GUI,那么这可能是一个更好的选择。

Here's a pagethat discusses some trade-offs and demonstrates some helpful tools to use when designing your layouts. It mostly talks about RelativeLayout and LinearLayout, but is also apropos to your choice between RelativeLayout and Framelayout. Just keep in mind that FrameLayout is an even simpler layout.

这是一个页面,讨论了一些权衡并演示了一些在设计布局时使用的有用工具。它主要讨论RelativeLayout 和LinearLayout,但也适合您在RelativeLayout 和Framelayout 之间进行选择。请记住,FrameLayout 是一种更简单的布局。

Edit (2017): For even more complicated layouts, you may be able to avoid nested layouts by using ConstraintLayout.

编辑(2017):对于更复杂的布局,您可以使用 ConstraintLayout 来避免嵌套布局。