【问题标题】:Difference between Frame and Relative layout?框架和相对布局之间的区别?
【发布时间】:2012-07-21 17:28:12
【问题描述】:

我是 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>

【问题讨论】:

标签: android android-layout android-relativelayout android-framelayout


【解决方案1】:

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 的布局编辑器一起使用。

【讨论】:

    【解决方案2】:

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

    FrameLayout 允许沿 Z 轴放置。也就是说,您可以将视图元素一层一层堆叠。

    【讨论】:

    • 你也可以在RelativeLayout中堆叠
    【解决方案3】:

    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"
    

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

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2018-06-22
      • 2011-05-05
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多