用于叠加层的 FrameLayout 与 RelativeLayout [英] FrameLayout vs RelativeLayout for overlays

查看:28
本文介绍了用于叠加层的 FrameLayout 与 RelativeLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

My guess was to use FrameLayout for 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 那么,如果不是在这种情况下?如果我采用 FrameLayout 方式有什么缺点?

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

推荐答案

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

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

针对您的问题,RelativeLayout 比简单得多的 FrameLayout 更大,功能更强大.所以对于简单的布局,后者可能更有效.但是,如果使用 RelativeLayout 及其添加的定位选项允许您在较少的布局视图中实现 GUI,那么这可能是更好的选择.

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 和LinearLayout,但也适合您在RelativeLayout 和Framelayout 之间进行选择.请记住,FrameLayout 是一种更简单的布局.

Here's a page that 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.

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

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

这篇关于用于叠加层的 FrameLayout 与 RelativeLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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