9

私はAndroidプログラミングに不慣れですが、ドキュメントのレイアウトをどれだけ理解しているかから、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>
4

3 に答える 3

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 の Layout Editor で簡単に使用できます。

于 2012-07-23T13:17:03.590 に答える
10

ビューの関係に基づくRelativeLayout 。何らかのルールに基づいて UI 要素を配置するのに役立つレイアウト マネージャーです。次のようなものを指定できます: これを親の左端に揃える、これをこの要素の左/右に配置するなど。

FrameLayoutでは、Z 軸に沿って配置できます。つまり、ビュー要素を上下に積み重ねることができます。

于 2015-06-01T08:15:22.817 に答える