Androidレイアウト大全


Androidは、FrameLayout(フレームレイアウト)、LinearLayout(線形レイアウト)、AbsoluteLayout(絶対レイアウト、2.33バージョン以前、その後除去)、RelativeLayout(相対レイアウト)、TableLayout(テーブルレイアウト)の5つのレイアウトオブジェクトを使用する.
FrameLayout:すべてのコンポーネントをグループ化して解き、1つは1つを上書きします.
画面上の空白の代替領域としてカスタマイズされ、パブリッシュする画像など、単一のオブジェクトを埋め込むことができます.すべてのサブエレメントは画面の左上隅に固定されます.FrameLayoutのサブエレメントの位置を指定することはできません.後のサブエレメントは、前のサブエレメントの上に直接上書きされ、後のサブエレメントが透明でない限り、それらの一部またはすべてをブロックします.
例:
 
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

 <!--          Button   -->
<Button
    android:text="button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>
<TextView
    android:text="textview"
    android:textColor="#0000ff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
</FrameLayout> 

LinearLayout: と の2 に けられ、1 レイアウトのLinearLayoutは、 または のプロパティ ですべてのサブエレメントを べます.すべてのサブエレメントは のエレメントの にスタックされるため、 リストの には1つのエレメントしかありません. にかかわらず、 リストには1つの の さ( さが も いサブエレメントの さに の さ)しかありません.LinearLayoutは、サブエレメント の を し、 いに わせします(1つのエレメントに して 、 、または ).LinearLayoutでは、 のサブエレメントにweightを することもサポートされています. は、サブエレメントが の りのスペースを めることを にすることです.これにより、 きな で さなオブジェクトが みになることを し、 を して めることができます.サブエレメントはweight を し、 りのスペースはサブエレメントが したweightスケールでサブエレメントに り てられます.デフォルトのweight は0です.たとえば、weight が1である3つのテキストボックスがある 、2つのテキストボックスは に され、 りのスペースが たされますが、3 のテキストボックスは されません.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="2">
    <TextView
        android:text="Welcome to Mr Wei's blog"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     />
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
   
    <TextView
        android:text="red"

        android:gravity="center_horizontal" //       
        android:background="#aa0000"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>
    <TextView
        android:text="green"
        android:gravity="center_horizontal "
        android:background="#00aa00"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>   
    </LinearLayout>
</LinearLayout>

RelativeLayout: コンポーネントによって のコンポーネントの が されます.RelativeLayoutでは、 が の または に する を できます(IDで します).したがって、 え、 、 に して2つの を べることができます. は に べられているので、 の が の にある 、その に する の は の の な で べられます.XMLを してこのlayoutを する は、 する に する を する があります.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Mr Wei's blog:"/>
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="OK" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout> 

TableLayout: の テーブルレイアウトで、TableRowは を し、 にコンポーネントを できます.TableLayoutは、サブ の を または に り てます.1つのTableLayoutは くのTableRowからなり、 TableRowはrowを します( には、 のサブオブジェクトを することができます.これは で します).TableLayoutコンテナにはrow、cloumns、cellの は されません. rowは0 のcellを する. cellにはViewオブジェクトがあります. は と で された くのセルです. では、セルを にすることができます.セルは、HTMLとは なる にまたがることはできません.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <TextView android:layout_column="1" android:text="Open..." />
        <TextView android:text="Ctrl-O" android:gravity="right" />
    </TableRow>
    <TableRow>
        <TextView android:layout_column="1" android:text="Save..." />
        <TextView android:text="Ctrl-S" android:gravity="right" />
    </TableRow>
    <View android:layout_height="2dip" android:background="#FF909090" /> //          
    <TableRow>
        <TextView android:text="X" />
        <TextView android:text="Export..." />
        <TextView android:text="Ctrl-E" android:gravity="right " />
    </TableRow>
    <View android:layout_height="2dip" android:background="#FF909090" />
    <TableRow>
        <TextView android:layout_column="1" android:text="Quit"
            android:padding="3dip" />
    </TableRow>
</TableLayout>