Lesson 8 (Gravity & Layout Gravity)
ကြၽန္ေတာ္ lesson 7 မွာေၾကာ္ျငာခဲ့တဲ့အတိုင္း Gravity နဲ႔ Layout Gravity အေၾကာင္းတင္ေပးမွာ ျဖစ္ပါတယ္။
Gravity ဆိုတာ Item ေတြကို အလယ္ ဘယ္ ညာ အေပၚ ေအာက္ ထားတာပါ။ ဥပမာ Layout တစ္ခုထဲမွာ Button တစ္ခုပါမယ္ဗ်ာ။ အဲ့Buttonကို Layout ရဲ႕ အေပၚမွာထားမလား အလယ္မွာထားမလား ေအာက္မွာထားမလား။ ကိုယ္ထားခ်င္တဲ့ေနရာကို Gravity နဲ႔သံုးၿပီိးထားပါတယ္။ Gravity ကို Linear Layout မွာပဲသံုးလို႔ရပါတယ္။
ဥပမာနဲ႔ေျပာမယ္ေနာ္။ Button တစ္ခုကို အေပၚမွာထားခ်င္တယ္ဆိုရင္
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
အဲ့လိုထည့္ရပါတယ္။
android:gravity ကိုစစလုပ္ခ်င္း project အသစ္လုပ္တိုင္း center ေပးထားပါတယ္။ အဲ့code ေတြကို Run ရင္ အလယ္တည့္တည့္မွာ ခလုပ္တစ္ခုရပါတယ္။ android:gravity ကို Limear Layout ထဲမွာထည့္ထားလို႔ </LinearLayout> မပိတ္မခ်င္းၾကားထဲမွာ ႐ွိတဲ့ item ေတြအကုန္လံုး အလယ္မွာပဲ ႐ွိေနပါလိမ့္မယ္။ ေနာက္တစ္မ်ိဳးလုပ္ၾကည့္တာေပါ့။
Button ကိုအေပၚမွာထားမယ္ဆိုရင္ center ေနရာမွာ top လို႔ထည့္လိုက္ပါ။
ဘယ္ဘက္မွာကပ္ခ်င္ရင္ left
ညာဖက္မွာကပ္ခ်င္ရင္ right
အေပၚမွာဆို top
ေအာက္ဖက္ကပ္ရင္ bottom
ေဒါင္လိုက္အလယ္ဆိုရင္ center_vertical
အလ်ားလိုက္အလယ္ဆိုရင္ center_horizontal
အဲ့key ေလးေတြ ထည့္ၿပီး စမ္းၾကည့္ပါ။ ေနာက္ထပ္႐ွိပါေသးတယ္။ အသံုးနည္းလို႔မေျပာျပေတာ့တာပါ။
•Layout Gravity
Gravity မွာက center လို႔ေခၚလိုက္ရင္ အကုန္လံုးက အလယ္မွာခ်ည္းေရာက္ေနတယ္ဗ်ာ။ အခုကိုယ္လိုခ်င္တာက ဘယ္မွာတစ္ခု ညာမွာ တစ္ခုဆိုရင္ေတာ့ Layout gravity ကိုသံုးပါတယ္။ အေပၚက tutorial မွာလုပ္ခဲ့တဲ့ android:gravity="center" ဆိုတဲ့စာေၾကာင္းကိုဖ်က္လိုက္ပါ။
ၿပီးရင္
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"/>
</LinearLayout>
အဲ့တိုင္းထည့္လိုက္ပါ။ အဲ့က်ရင္ ဘယ္တစ္ခု ညာတစ္ခုျဖစ္ေနပါလိမ့္မယ္။
မ်ားေသာအားျဖင့္ Layout Gravityကို Layout ႏွစ္ဆင့္သံုးရင္ ပိုၿပီးအသံုးမ်ားပါတယ္။ ဥပမာ Linear Layout ထဲမွာ Linear Layout ထပ္ထည့္မယ္ဆိုရင္ Layout Gravity ကိုသံုးပါတယ္။ အေသးစိတ္ကို ေနာက္ tutorial ေတြမွာ အင့္ရင္သင့္သလို ႐ွင္းျပေပးသြားပါမယ္။
မ႐ွင္းလင္းတာေတြကို Comment မွာေမးသြားႏိုင္ပါတယ္။
သင္၏comment တစ္ေၾကာင္းသည္ ကြၽန္ုပ္အတြက္ ေနာက္ပိုစ့္မ်ားတင္ရန္ အားေဆးတစ္ခြက္ ျဖစ္ေစပါလိမ့္မည္။
