Android布局-相对与框架
我有一个应用程序,我想实现一个布局,其中产品图像将位于背景中,顶部位于右下角,我想放置产品的价格,我想在左下角放置一个添加按钮。
我应该使用框架布局还是相对布局???
I have an application where I want to achieve a layout where the product image will be in the background and top of that to the right bottom, I want to place the price of the product and to the left bottom I want to place an add button.
Should I use frame layout or relative layout ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为一个实际规则,我认为这取决于你。我个人倾向于使用RelativeLayouts,因为它们更灵活,但您可以使用其中任何一个实现相同的效果。
这篇 SO 文章更详细地解释了两种布局之间的性能差异:FrameLayout 与相对布局的覆盖
As a practical rule, I think it's up to you. I personally tend to use RelativeLayouts because they're more flexible, but you can achieve the same effect with either.
This SO post explains the performance differences between the two layouts in more detail: FrameLayout vs RelativeLayout for overlays
相对布局:当兄弟姐妹或父母之间有关系时。
框架布局:当兄弟姐妹彼此独立放置并且仅依赖于父级时。
根据您的情况,您可以选择其中任何一个。
Relative layout: When you have relation between siblings or parent.
Frame layout: When siblings are placed independent of each other and are only dependent on parent.
Based on your situation, you can opt in of any of these.
我的建议是,如果您对每个设备的背景都有特定的尺寸,那么请采用相对布局,并将背景设置为图像。因为在
RelativeLayout
中使用背景时,无论图像是否适合RelativeLayout
的大小,它都会适合相对布局本身的大小。 (可以拉伸/像素化/未正确添加)如果您不确定具体尺寸,您应该使用
FrameLayout
和ImageView
来处理ScaleType
,可以是centerCrop
,它将以良好的形状适合布局。对于
TextView
,使用layout_gravity
,它将根据父布局处理位置。My advice, If you have specific size for your background for each device, then go for relative layout, and set the background to your image. Because when using background in
RelativeLayout
, It'll fit the size of the relative layout itself, whether the image suits the size of theRelativeLayout
or not. (Can be stretched/pixelated/Not properly added)If you're not sure about specific size, you should use
FrameLayout
, withImageView
, that handles theScaleType
, which can becenterCrop
, and it'll fit the layout in good shape.And for the
TextView
, uselayout_gravity
, which will handle the position based on the parent layout.