2012-07-23 57 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" 

使用上面的屬性子視圖位置被修改。

相關問題