Android पर गुरुत्वाकर्षण और layout_gravity के बीच अंतर
पृथ्वी का गुरुत्वाकर्षण बल कितनी ऊंचाई तक है (12)
मुझे पता है कि हम android:gravity
को निम्न मान सेट कर सकते हैं android:gravity
और android:layout_gravity
गुण:
-
center
-
center_vertical
-
center_horizontal
, आदि
लेकिन मैं इन दोनों के बारे में उलझन में हूं।
android:gravity
के उपयोग के बीच क्या अंतर है android:gravity
और android:layout_gravity
?
अंतर
android:layout_gravity
के बाहरी गुरुत्वाकर्षण है। उस दिशा को निर्दिष्ट करता है जिसमें दृश्य को अपने माता-पिता की सीमा को छूना चाहिए।
android:gravity
उस दृश्य के अंदर गुरुत्वाकर्षण है। निर्दिष्ट करता है कि किस दिशा में इसकी सामग्री को संरेखित करना चाहिए।
एचटीएमएल / सीएसएस समकक्ष
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
आपको याद रखने में मदद करने के लिए आसान चाल
layout-gravity
को "ले- layout-gravity
" के रूप में लें।
इसे याद रखने के लिए एक आसान चाल गुरुत्वाकर्षण पृथ्वी के अंदर हमारे लिए लागू होती है। तो, android:gravity
दृश्य के अंदर है।
_gravity को बाहर रखने में बाहर निकलें जो आपको android:layout_gravity
को याद रखने में मदद करेगा android:layout_gravity
दृश्य के बाहर संदर्भित करेगा
गुरुत्वाकर्षण का उपयोग दृश्य संरेखण को दृश्यों में सेट करने के लिए किया जाता है लेकिन लेआउट_ग्रैविटी का उपयोग इसे स्वयं सेट करने के लिए किया जाता है। आइए उदाहरण लें कि अगर आप एडिटटेक्स्ट में लिखे गए पाठ को संरेखित करना चाहते हैं तो गुरुत्वाकर्षण का उपयोग करें और आप इस एडिट टेक्स्ट या किसी बटन या किसी भी दृश्य को संरेखित करना चाहते हैं तो लेआउट_ग्रैविटी का उपयोग करें, तो यह बहुत आसान है।
दोनों के बीच बुनियादी अंतर यह है कि-
एंड्रॉइड: दृश्य के बाल तत्वों के लिए गुरुत्वाकर्षण का उपयोग किया जाता है।
एंड्रॉइड: लेआउट_ग्रैविटी का उपयोग मूल तत्व के संबंध में इस तत्व के लिए किया जाता है।
यदि हम किसी दृश्य के अंदर सामग्री की गुरुत्वाकर्षण सेट करना चाहते हैं तो हम "एंड्रॉइड: गुरुत्वाकर्षण" का उपयोग करेंगे, और यदि हम अपने दृष्टिकोण के भीतर इस दृश्य (पूरी तरह से) की गुरुत्वाकर्षण सेट करना चाहते हैं तो हम "एंड्रॉइड" layout_gravity "।
यद्यपि प्रश्न पहले से ही उत्तर दिया गया है, मेरे पास गुरुत्वाकर्षण, लेआउट_ग्रैविटी और लेआउट_वेट के उपयोग का प्रदर्शन करने वाले कुछ नमूने हैं।
आप http://juanpickselov.com/LayoutExamples.zip पर उदाहरण पा सकते हैं
मैंने ग्रहण में फ़ाइलों को बनाया, .svn सबफ़ोल्डर को हटा दिया और शैलियों, तारों, रंगों आदि शामिल हैं। लेआउट फ़ाइलें डेमो का मुख्य बिंदु हैं। चूंकि मैं जावा और एंड्रॉइड डेवलपमेंट न्यूबी हूं, इसलिए कोई जावा अक्षम हो सकता है। फ़ाइलों को ग्रहण परियोजना में कॉपी किया जा सकता है या मैंने उन आईडीई के लिए एंड्रॉइड डेवलपमेंट प्लगइन के साथ नेटबींस में भी इसका इस्तेमाल किया है।
सैंडिप के ब्लॉग पर मैंने जो कुछ देखा, जिसे मैंने लगभग याद किया, मेरी समस्या तय की। उन्होंने कहा कि layout_gravity
LinearLayout
साथ काम नहीं करता है।
यदि आप LinearLayout
का उपयोग कर रहे हैं और गुरुत्वाकर्षण सेटिंग्स आपको पागल कर रही हैं (मेरे जैसे), तो कुछ और पर स्विच करें।
मैं वास्तव में एक layout_alignParentLeft
में स्विच किया गया था RelativeLayout
फिर layout_alignParentLeft
और layout_alignParentRight
2 निहित layout_alignParentRight
पर उन्हें एक लाइन पर दूर बाएं और बहुत दूर जाने के लिए इस्तेमाल किया था।
-
gravity
दृश्य के अंदर सामग्री की व्यवस्था करता है। -
lay out _gravity
दृश्य की स्थिति खुद के बाहर व्यवस्थित करता है।
कभी-कभी यह एक तस्वीर देखने में भी मदद करता है। हरे और नीले रंग के TextViews
और अन्य दो पृष्ठभूमि रंग LinearLayouts
।
टिप्पणियाँ
-
layout_gravity
में विचारों के लिए काम नहीं करती है। एकLinearLayout
याFrameLayout
में विचारों के लिए इसका इस्तेमाल करें। अधिक जानकारी के लिए मेरे पूरक उत्तर देखें। - दृश्य की चौड़ाई (या ऊंचाई) को इसकी सामग्री से अधिक होना चाहिए। अन्यथा
gravity
का कोई प्रभाव नहीं पड़ेगा। इस प्रकार,wrap_content
औरgravity
एक साथ अर्थहीन हैं। - दृश्य की चौड़ाई (या ऊंचाई) को माता-पिता से कम होना चाहिए। अन्यथा
layout_gravity
का कोई प्रभाव नहीं पड़ेगा। इस प्रकार,match_parent
औरlayout_gravity
एक साथ अर्थहीन हैं। -
layout_gravity=center
layout_gravity=center_horizontal
जैसा ही दिखता है क्योंकि वे लंबवत रैखिक लेआउट में हैं। आप इस मामले में लंबवत केंद्र नहीं कर सकते हैं, इसलिएlayout_gravity=center
केवल क्षैतिज रूप से केंद्रित है। - यह उत्तर केवल लेआउट के भीतर विचारों पर
gravity
औरlayout_gravity
सेट करने के साथlayout_gravity
गया है। यह देखने के लिए कि जब आप पैरेंट लेआउट केgravity
को स्वयं सेट करते हैं तो क्या होता है, ऊपर दिए गए पूरक उत्तर को देखें। (सारांश:gravity
एकRelativeLayout
LinearLayout
पर अच्छी तरह से काम नहीं करता है लेकिन एकLinearLayout
साथ उपयोगी हो सकता है।)
तो याद रखें, लेआउट _ गुरुत्वाकर्षण अपने लेआउट में एक दृश्य की व्यवस्था करता है। गुरुत्वाकर्षण दृश्य के अंदर सामग्री की व्यवस्था करता है।
एक्सएमएल
आपके संदर्भ के लिए उपर्युक्त छवि के लिए xml यहां दिया गया है:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e3e2ad"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="layout_gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</LinearLayout>
</LinearLayout>
सम्बंधित
- व्यू के पैडिंग और मार्जिन के बीच अंतर
- Match_parent बनाम wrap_content
- प्रोग्रामर के रूप में एक LinearLayout दोनों गुरुत्वाकर्षण और लेआउट गुरुत्वाकर्षण सेट करने के लिए कैसे
gravity
और layout-gravity
में बहुत अंतर है। मैं इन 2 अवधारणाओं के बारे में अपने अनुभव की व्याख्या करने जा रहा हूं ( मेरे अवलोकन और कुछ वेबसाइटों के कारण मुझे मिली सभी जानकारी )।
FrameLayout
में गुरुत्वाकर्षण और लेआउट-गुरुत्वाकर्षण का उपयोग .....
ध्यान दें:-
गुरुत्वाकर्षण का उपयोग व्यू सामग्री के अंदर किया जाता है क्योंकि कुछ उपयोगकर्ता का उत्तर होता है और यह सभी व्यू
ViewGroup Layout
लिए समान होता है।Layout-gravity
का उपयोग माता-पिता के साथ किया जाता है क्योंकि कुछ उपयोगकर्ता के पास उत्तर होता है।Gravity and Layout-gravity
FrameLayout
बच्चों के साथ अधिक उपयोगी कामFrameLayout
।We can't use Gravity and Layout-gravity
फ्रेमलेआउट के टैग मेंWe can't use Gravity and Layout-gravity
....layout-gravity
का उपयोग करकेFrameLayout
में कहीं भी चाइल्ड व्यू सेट कर सकते हैं।हम
center_vertical
अंदर गुरुत्वाकर्षण के प्रत्येक मूल्य का उपयोग कर सकते हैं (उदाहरण: -center_vertical
,center_horizontal
,center
,top
, आदि), लेकिन अन्यcenter_vertical
center_horizontal
लेआउट के साथ यह संभव नहीं है।FrameLayout
Layout-gravity
पूरी तरहLayout-gravity
पर काम कर रहा है। उदाहरण: - यदि आपFrameLayout
पर काम करते हैं तो आपको नया दृश्य जोड़ने के लिए पूरे लेआउट को बदलने की आवश्यकता नहीं है । आप केवलFrameLayout
में अंतिम रूप में दृश्य जोड़ें और उसेLayout-gravity
मूल्य के साथ दें। ( यह फ्रेमलेआउट के साथ लेआउट-गुरुत्वाकर्षण के फायदे हैं )।
उदाहरण पर देखें ......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center"
android:text="Center Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#1b64b9"
android:gravity="bottom"
android:layout_gravity="bottom|center"
android:text="Bottom Layout Gravity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#d75d1c"
android:gravity="top"
android:layout_gravity="top|center"
android:text="Top Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="100dp"
android:textColor="#d71f1c"
android:gravity="top|right"
android:layout_gravity="top|right"
android:text="Top Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="Top Left Layout Gravity"/>
</FrameLayout>
आउटपुट: -
LinearLayout में गुरुत्वाकर्षण और लेआउट-गुरुत्वाकर्षण का उपयोग .....
Gravity
उपर्युक्त के समान काम करता है लेकिन यहां भिन्नता यह है कि हम LinearLayout View
और RelativeLayout View
LinearLayout View
अंदर गुरुत्वाकर्षण का उपयोग कर सकते हैं जो FrameLayout View
में संभव नहीं है।
अभिविन्यास लंबवत के साथ LinearLayout ....
नोट: - यहां हम layout_gravity
केवल 3 मान सेट कर सकते हैं ( left
| right
| center
(जिसे center_horizontal
भी कहा जाता है))।
उदाहरण पर देखें: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="right"
android:text="Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="Left Layout Gravity"/>
</LinearLayout>
आउटपुट: -
अभिविन्यास क्षैतिज के साथ LinearLayout ....
नोट: - यहां हम layout_gravity
3 मान भी सेट कर सकते हैं ( top
| bottom
| center
(जिसे center_vertical
भी कहा जाता है)।
उदाहरण पर देखें: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Bottom \nLayout \nGravity"/>
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="center"
android:text="Center \nLayout \nGravity"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Left \nLayout \nGravity"/>
</LinearLayout>
उत्पादन: -
नोट: - हम RelativeLayout Views
में layout_gravity
का उपयोग नहीं कर सकते हैं, लेकिन हम layout_gravity
बच्चों को एक ही स्थिति में सेट करने के लिए gravity
का उपयोग कर सकते हैं ....
android:gravity
का उपयोग ऑब्जेक्ट के भीतर ऑब्जेक्ट की सामग्री को कैसे रखा जाए, यह निर्दिष्ट करने के लिए किया जाता है। एक और शब्द में, एंड्रॉइड: गुरुत्वाकर्षण का उपयोग दृश्य की सामग्री के गुरुत्वाकर्षण को निर्दिष्ट करने के लिए किया जाता है।
android:layout_gravity
एक android:layout_gravity
है जिसे बच्चा अपने माता-पिता के भीतर गुरुत्वाकर्षण के दृश्य को निर्दिष्ट करने के लिए अपने माता-पिता को आपूर्ति कर सकता है।
अधिक जानकारी के लिए आप जा सकते हैं
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html
लेआउट_ग्रैविटी जो मैं इकट्ठा कर सकता हूं उससे उसके माता-पिता के अंदर उस दृश्य की गुरुत्वाकर्षण है, और गुरुत्वाकर्षण उस दृश्य के अंदर बच्चों की गुरुत्वाकर्षण है।
मुझे लगता है कि यह सही है लेकिन पता लगाने का सबसे अच्छा तरीका आसपास खेलना है।
गुरुत्वाकर्षण के बारे में स्पष्ट होने के लिए छवि को देखें