Professional Documents
Culture Documents
สาระบทเรียน
ต่อไปให้ ใช้ รหัสคาสัง่ เดิม แต่เปลี่ยนแนวจากแนวดิง่ ให้ เป็ นแนวนอน โดยเปลี่ยนจาก Vertical มา
เป็ น Horizontal แทน แล้ วลองรันดู พร้ อมสังเกตุผลลัพธ์ที่ได้ มา
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
ส่วนแสดงผลแบบรี เลทีฟเลย์เอาต์ เป็ นส่วนแสดงผลที่ กาหนดให้ ตอนเริ่ มต้ นเมื่อสร้ างโปรเจ็กต์ แอน
ดรอยด์ขึ ้นมาด้ วยอิลิเมนต์ <RelativeLayout>...</RelativeLayout> มีการจัดเรี ยงอิลิเมนต์ที่อยู่ภายใน
โดยอ้ างอิงตาแหน่งของอิลิเมนต์นนๆ ั ้ กับอิลิเมนต์อื่นๆ ในเลย์เอาต์ หรื ออาจจะอ้ างอิงกับตัวเลย์เอาต์ที่อิลิ
เมนต์นนอยู
ั ้ ่ภายในก็ได้ ซึ่งสามารถอ้ างอิงผ่านไอดี (id) ของอิลิเมนต์หรื อเลย์เอาต์ เช่น ปุ่ ม login ใน
แนวดิ่งให้ วางอยู่ใต้ ช่องรับรหัสผ่านห่างลงมา 20 dp แนวนอนให้ วางอยู่กลางจอ ซึ่งโดยรวมแล้ วน่าจะเป็ น
ระบบการจัดวางที่ยืดหยุน่ ที่สดุ มีข้อดีหลายอย่าง
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtusername"
android:layout_toRightOf="@+id/txtusername"
android:layout_alignParentRight="true"/>
3. วางข้ อความ Password โดยอ้ างอิงว่า ให้ อยู่ด้านล่างข้ อความ Username และด้ านซ้ ายเสมอเป็ น
แนวเดียวกับ Username ด้ วย ดังนี ้
<TextView
android:id="@+id/txtusername"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:text="Username"
android:textSize="20sp" />
<TextView
android:id="@+id/txtpassword"
87
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtusername"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="Password"
android:textSize="20sp" />
4. วาง EditText เป็ นช่องรับ password โดยอ้ างอิงว่า ให้ อยู่ขวามือและฐานแนวเดียวกับ password
ข้ อ 3 และด้ านซ้ ายจัดให้ อยูเ่ สมอแนวเดียวกับช่องรับ username ข้ อ 1
5. วางปุ่ ม Sign in โดยอ้ างอิงกับ Password หรื อจะอ้ างอิงกับ Username ก็ได้ โดยใช้ หลักการ
เดียวกัน
88
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_password"
android:layout_centerHorizontal="true"
android:text="Sing In"/>
โดยลักษณะการอ้ างอิงจะเป็ นทอดๆ ตามลูกศร ดังรูป โดยขณะนี ้เราให้ Username เป็ นตัวหลัก ถ้ าขยับ
Username ตัวอื่นก็จะขยับลงทังแผง
้ เมื่อ run จะได้ ผลตามภาพด้ านล่าง
89
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@android:drawable/alert_dark_frame"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="สวัสดีแอนดรอย์ KU"
android:textSize="24sp"
android:textColor="#ffff"
android:layout_height="fill_parent"
90
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/next"
android:text="Next"
android:layout_x="10px"
android:layout_y="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_x="19dp"
android:layout_y="74dp"
android:text="First Name"
android:textSize="18sp"
92
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_x="140dp"
android:layout_y="54dp"
android:width="300px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_x="22dp"
android:layout_y="137dp"
android:text="Last Name"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_x="143dp"
android:layout_y="117dp"
android:width="300px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</AbsoluteLayout>
และเมื่อดูกราฟิ กในแอนดรอยด์จะพบว่า Graphical Layout จะเห็นอิลิเมนต์ตา่ งๆ ซ้ อนกันอยู่ เป็ นชันๆ
้ ซึง่
เป็ นลักษณะของแอบโซลูตเลย์เอาต์
93
และเมื่อรันก็จะแสดงผลลัพธ์ ตามภาพ
android:layout_height="wrap_content">
</TextView>
<EditText android:id="@+id/editText2"
android:layout_width="200dip"
android:layout_height="wrap_content">
</EditText>
</TableRow>
<TableRow
android:layout_height= "wrap_content"
android:layout_width= "wrap_content"
android:id= "@+id/tableRow3">
<TextView android:text=""
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:layout_width="wrap_content"
android:text="Login"
android:id="@+id/button1"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
และเมื่อรันก็จะแสดงผลลัพธ์ตามภาพ
3. ให้ นิสิตสร้ าง Layout ตามภาพนี ้ โดยให้ เขียนในรูปแบบ Linear Layout , Relative Layout ,
Frame Layout, Absolute Layout, Table Layout