例)左右両端にボタンを配置する場合、layout_alignParentを使います。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/name" />
</RelativeLayout>
例)フォーム入力のように右揃えで揃える場合、まずボタンの最大サイズを表す見えないダミーボタンを作り、そのボタンの右端で整列させて、テキストをそのボタンの右側に配置します。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/base1"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:visibility="invisible" />
<Button
android:id="@+id/button1"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignRight="@id/base1"
android:text="@string/name" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:layout_centerVertical="true"
android:text="@string/myname" />
</RelativeLayout>
また、このレイアウトをLinearLayoutで縦に並べていけば下記のようなレイアウトを作れます。「xmlレイアウトをaddViewで追加する」のようにプログラムで追加すれば任意の数のフォームを作るような機能をつけることもできます。
参考:Android Developers:RelativeLayout


No comments:
Post a Comment