Posted by : Wai Hlyan Sunday, January 17, 2016

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 တစ္​​ေၾကာင္​းသည္​ ကြၽန္​ုပ္​အတြက္​ ​ေနာက္ပိုစ္​့မ်ားတင္​ရန္​ အား​ေဆးတစ္​ခြက္​ ျဖစ္​​ေစပါလိမ္​့မည္​။

Leave a Reply

Subscribe to Posts | Subscribe to Comments

လာ​ေရာက္​ၾကည္​့႐ွႈသူမ်ား

Welcome to My Blog

Zawgyi-Unicode

Designed by အိမ္​​ေ႐ွ႕မင္​းသား-နည္​းပညာ. Powered by Blogger.

Random Posts

Contact Form

Name

Email *

Message *

Thankful

Popular Posts

- Copyright © Myanmar Android Tutorials For Beginners -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -