框架和相对布局之间的区别?

时间:2012-07-23 13:09:08

标签: android android-layout relativelayout android-framelayout

我是Android编程的新手,但从我对documentation的布局了解多少,当您需要基于某些规则的视图时,RelativeLayout主要用于想要重叠视图时的FrameLayout

但遗憾的是,对于以下程序,我使用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>

3 个答案:

答案 0 :(得分:18)

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"

确保视图阵容相对于彼此正确。 FrameLayout非常相似,只是它只使用重力来显示其视图(没有关系)。

我还建议您查看ConstraintLayout组件。 ConstraintLayout允许您使用平面视图层次结构创建大型复杂布局(无嵌套视图组)。它与RelativeLayout类似,因为所有视图都根据兄弟视图和父布局之间的关系进行布局,但它比RelativeLayout更灵活,更易于与Android Studio的布局编辑器一起使用。

答案 1 :(得分:10)

RelativeLayout 基于观点的关系。它是一个布局管理器,可帮助您根据某些规则排列UI元素。您可以指定以下内容:将此对齐到父项左边缘,将其放在此元素的左侧/右侧等。

FrameLayout 允许沿Z轴放置。也就是说,您可以将视图元素叠加在另一个之上。

答案 2 :(得分:0)

RelativeLayout - 正如此视图组中的名称所示,视图相对于彼此放置。最常用的relativelayout属性是

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"

视图相对于彼此放置。在开发复杂设计时非常有用。

FrameLayout - 它表现为单个对象视图不是相对于每个而是放置在FrameLayout上。 FrameLayout采用最大子视图的大小。

android:gravity="center_horizontal|center_vertical|bottom"

使用上面的属性子视图位置被修改。