2012-07-23 58 views
6

我是新来的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> 
+0

http://stackoverflow.com/a/4905403/1434631 – Nermeen 2012-07-23 13:11:56

回答

16

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

8

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

FrameLayout允许沿Z轴进行布局。那就是你可以将你的视图元素叠加在一起。

+2

你也可以在RelativeLayout中堆栈 – NikkyD 2015-11-11 12:29:46

0

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" 

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

相关问题