2012-07-23 14 views
6

私はアンドロイドプログラミングに慣れていますが、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

答えて

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

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" 

上記のプロパティの子ビューの使用位置が変更されました。

関連する問題