2

私はこの結果を得る方法を考えていました:

http://img718.imageshack.us/img718/6173/screenshot20100411at126.png

これは私が試したものです:

<RelativeLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="#CCCCCC"
                  android:orientation="horizontal"
                  android:layout_weight="0">

        <Button android:id="@+id/btn_cancel"
                android:text="@string/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5px"
                android:gravity="left"
                />

        <Button android:id="@+id/btn_save"
                android:text="@string/save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5px"
                android:layout_toRightOf="@id/btn_cancel"
                android:gravity="right"/>
    </RelativeLayout>

私はlayout_widthプロパティを試して、両方の親を埋めるように設定しようとしましたが、誰かがアイデアを持っている場合は機能しませんでした...ありがとうございます!

4

1 に答える 1

4
<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:padding="5dip"
              android:background="#CCCCCC"
              android:orientation="horizontal">

    <Button android:id="@+id/btn_cancel"
            android:text="@string/cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:layout_weight="1"/>

    <Button android:id="@+id/btn_save"
            android:text="@string/save"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
</LinearLayout>

2 つのウィジェットが同じ量のスペースを占めるようにする LinearLayout の秘訣は、 layout_width="fill_parent"それらの両方に設定してから、両方にも設定layout_weight="1"することです。両方を fill_parent と同じ layout_weight に設定すると、使用可能なすべてのスペースを 2 つに分割するよう LinearLayout に指示されます。

また、dipの代わりに を使用しpxます。 dipは画面サイズに依存せず、さまざまなサイズの画面で見栄えがよくなります。

于 2010-04-12T06:49:32.947 に答える