1. เลือกเมนู Files > New > Android Project
2. พิมพ์ว่า My App แล้วกด Next ไปเรื่อยๆ แล้วกด Finish
3. ไฟล์ activity_main.xml ให้ใส่ Code นี้
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
// เลือกใส่ layout ตาม code ด้านล่าง
</LinearLayout>
Linear Layout จัดวาง object ตามแนวตั้งหรือแนวนอน
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:background="#dddddd"
android:padding="5dp" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hor 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hor 2" />
</LinearLayout>
บรรทัดที่ 5 กำหนดเป็น แนวตั้ง (vertical) หรือแนวนอน (horizontal)
บรรทัดที่ 2 กำหนดความกว้างเท่าที่จำเป็น
บรรทัดที่ 4 กำหนดให้แสดง layout นี้ตรงกลาง
บรรทัดที่ 6 กำหนดสีพื้นหลัง
บรรทัดที่ 7 กำหนดขนาดกรอบ (padding) รอบๆ layout
Relative Layout จัดวาง object โดยอ้างอิงกันและกัน
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Relative 1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/button2"
android:text="Relative 3" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button1"
android:text="Relative 2" />
</RelativeLayout>
Frame Layout จัดวาง object โดยอ้างอิงจากมุมบนซ้าย
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dddddd" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="frame 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:text="frame 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="30dp"
android:text="frame 3" />
</FrameLayout>
Table Layout จัดวาง object โดยแบ่งเป็นแถวและคอลัมภ์
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dddddd" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 3" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_span="2"
android:text="Table 4" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="Table 5" />
</TableRow>
</TableLayout>
เส้นแบ่งบรรทัด ใช้ code นี้
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#666666" />
ScrollView ใช้คล่อม layout ทั้งหมด เพื่อให้สามารถเลื่อนขึ้นลงได้
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
26 พ.ค. 57