Android 框架和相对布局之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11613193/
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
Difference between Frame and Relative layout?
提问by WakingDeviL
I'm new to android programming but from how much I have understood of the layouts from the documentation, RelativeLayout is mostly used when you need the views based on some rules and the FrameLayout when you want to overlap views.
我是 android 编程的新手,但从我对文档中的布局的了解程度来看,RelativeLayout 主要用于当您需要基于某些规则的视图和 FrameLayout 当您想要重叠视图时。
But unfortunately for the following program I get the work of FrameLayout done by using RelativeLayout. I got my work done but for understanding, Am I missing something in the difference? Also, how did the buttons come over my image? (Even the other image is overlapping.)
但不幸的是,对于下面的程序,我使用RelativeLayout 完成了FrameLayout 的工作。我完成了我的工作,但为了理解,我是否遗漏了一些不同之处?另外,按钮是如何覆盖我的图像的?(即使是另一幅图像也是重叠的。)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ic_launcher"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignLeft="@id/imageView1"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="Login" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="Register" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="Try application" />
</LinearLayout>
</RelativeLayout>
回答by Slickelito
The RelativeLayout
can use :
将RelativeLayout
可以使用:
android:layout_toEndOf="@id/some_view"
android:layout_toStartOf="@id/some_view"
android:layout_above="@id/some_view"
android:layout_below="@id/some_view"
to make sure views lineup correctly in relation to each other. FrameLayout
is very similar except it's only using gravity to put display its views (with no relation).
以确保彼此正确地观看阵容。FrameLayout
非常相似,除了它仅使用重力来显示其视图(没有关系)。
I would also suggest you to take a look at the ConstraintLayout component. ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.
我还建议您查看 ConstraintLayout 组件。ConstraintLayout 允许您创建具有平面视图层次结构(无嵌套视图组)的大型复杂布局。它与RelativeLayout 类似,所有视图都是根据兄弟视图和父布局之间的关系进行布局的,但它比RelativeLayout 更灵活,并且更易于与Android Studio 的布局编辑器一起使用。
回答by Jawad Zeb
RelativeLayoutbased on relation of views. It is a layout manager that helps you arrange your UI elements based on some rule. You can specify things like: align this to parents left edge, place this to the left/right of this elements etc.
基于视图关系的RelativeLayout。它是一个布局管理器,可帮助您根据某些规则排列 UI 元素。您可以指定以下内容:将其与父元素的左边缘对齐,将其放置在此元素的左侧/右侧等。
FrameLayoutallows placements along Z-axis. That is you can stack your view elements one above the other.
FrameLayout允许沿 Z 轴放置。也就是说,您可以将视图元素一个一个地堆叠在另一个之上。
回答by Tulsi
RelativeLayout - As the name suggest in this viewgroup, view are placed relative to each other. Most used property of relativelayout are used are
相对布局 - 正如这个视图组中的名称所暗示的,视图是相对于彼此放置的。使用的相对布局的最常用属性是
android:layout_toLeftOf="@id/some_view1"
android:layout_toRightOf="@id/some_view2"
android:layout_above="@id/some_view3"
android:layout_below="@id/some_view4"
android:layout_toendof="@id/some_view5"
android:layout_tostartof="@id/some_view6"
View are placeed relative to each other. It is really helpful while developing complex designed.
视图相对放置。在开发复杂的设计时真的很有帮助。
FrameLayout - It behaves as a single object view are not placed relative to each but as per to the FrameLayout. FrameLayout takes the size of biggest child view.
FrameLayout - 它表现为单个对象视图不是相对于每个对象而是根据 FrameLayout 放置的。FrameLayout 采用最大子视图的大小。
android:gravity="center_horizontal|center_vertical|bottom"
Using above property child views position is modified.
使用上面的属性子视图位置被修改。