2015-10-12 9 views
12

मैं क्षैतिज linear layout में layout_gravity का उपयोग करने का प्रयास कर रहा हूं। लेकिन यह काम नहीं लग रहा है। मैं relative layout या किसी अन्य लेआउट का उपयोग करना चाहता हूं। मैं जानना चाहता हूं कि यह linear layout के साथ क्यों काम नहीं कर रहा है। मैंने पहले layout_gravityvertical रैखिक लेआउट में उपयोग किया है और यह जिस तरह से मैंने अपेक्षित तरीके से काम किया है।layout_gravity क्षैतिज रैखिक लेआउट में काम नहीं कर रहा है

<LinearLayout 
    android:id="@+id/shata" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
    <TextView 
     android:id="@+id/title_textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textColor="@color/black" 
     android:text="shata1" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     /> 
    <TextView 
     android:id="@+id/map_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="right" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="shata2"/>  
</LinearLayout> 

आप देख सकते हैं रेखीय लेआउट एक orientation=horizontal और width=match_parent है (यह सिर्फ जड़ लेआउट के अंदर है)। लेकिन टेक्स्टव्यू दोनों LinearLayout के बाईं ओर चिपके हुए प्रदर्शित होते हैं, भले ही मैंने क्रमशः layout_gravity=center और layout_gravity=right दिया है।

+0

क्योंकि TextView के लेआउट घिरा है यह काम नहीं करेगा 'wrap_content' नहीं' match_parent' – ImMathan

+0

रैखिक लेआउट आप चाहते हैं काम नहीं करता। आपको रिश्तेदार लेआउट का उपयोग करना चाहिए। आप सापेक्ष लेआउट का उपयोग क्यों नहीं करना चाहते हैं? – wisemann

+1

लाइनरलायआउट में लेआउट_ग्रैविटी केवल ऑब्जेक्ट्स को लंबवत ऑब्जेक्ट में बदल सकता है। यदि अभिविन्यास क्षैतिज है, तो आप ऊपर और नीचे निर्दिष्ट कर सकते हैं, बाएं और दाएं नहीं। –

उत्तर

25

लघु जवाब: क्या आप एक क्षैतिज LinearLayout है, तो प्रत्येक बच्चे को केवल centerlayout_gravitytop के मूल्यों, bottom, और हो सकता है।

इसका कारण यह है कि आपने पहले से ही LinearLayout को प्रत्येक बच्चे क्षैतिज रूप से बाएं से दाएं को निर्दिष्ट करने के क्रम में संरेखित करने के लिए कहा है, जिसका अर्थ यह है कि यह केवल आपको प्रत्येक बच्चे के लंबवत संरेखण को निर्दिष्ट करने देता है layout_gravity पैरामीटर, और इसके विपरीत लागू होता है जब आपका LinearLayout लंबवत है।

एक अच्छा लिंक यह बताता है कि विभिन्न गुरुत्वाकर्षण here कैसे काम करते हैं।

+0

मैंने पहले भी ऐसा सोचा था। लेकिन उस तर्क से, लंबवत रैखिकलायआउट केवल बाएं, दाएं और केंद्र के मूल्य लेना चाहिए। लेकिन ऊर्ध्वाधर लेआउट विशेषता तल पर भी प्रतिक्रिया करता है। – Ashwin

+1

मुझे एक ऐसा उदाहरण देखने में दिलचस्पी होगी जहां ऊर्ध्वाधर 'नीचे' का जवाब देता है, क्योंकि जहां तक ​​मेरी समझ जाती है, यह नहीं होना चाहिए, और यह अन्य बच्चों को दिए गए वजन का एक आर्टेफैक्ट हो सकता है। –

0

आप लेआउट को 3 भाग में विभाजित कर सकते हैं, फिर सही भाग में "shata2" को मध्य भाग में "shata2" डालें और बाएं हिस्से को एक पारदर्शी दृश्य डालें।

<LinearLayout 
    android:id="@+id/shata" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

    <TextView 
     android:id="@+id/title_textview" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center|top" 
     android:layout_weight="1" 
     android:text="shata1" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#111" /> 

    <TextView 
     android:id="@+id/map_imageview" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="right" 
     android:gravity="right" 
     android:text="shata2" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 



</LinearLayout> 

लेकिन जब आप रैखिक लेआउट का उपयोग करते हैं तो आपके कोड में यह केवल एक के बाद विचार डालता है और यह लेआउट गुरुत्वाकर्षण की परवाह नहीं करता है। उम्मीद है कि यह उपयोगी होगा।

2

यदि आप अपने बच्चे को केंद्रित करना चाहते हैं तो आपको बच्चों के लिए अपने रैखिक लेआउट गुरुत्वाकर्षण देने की आवश्यकता है। आपके रैखिक लेआउट पर निम्नलिखित सेट करें:

android:gravity="center" 

मुझे आशा है कि मुझे आपका प्रश्न सही मिलेगा।

0
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal"> 
<TextView 
    style="@style/Base.TextAppearance.AppCompat.Headline" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Blankets &amp; Carpets" /> 

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/list_home_feature" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="30dp" 
    android:layout_marginRight="10dp"> 

</ListView> 

</LinearLayout> 
2

Matt Taylor's response के अतिरिक्त के रूप में, आप layout_weight संपत्ति का उपयोग कर लेआउट के अंदर तत्वों का आकार अनुपात निर्दिष्ट कर सकते हैं। अधिक जानकारी के लिए यह thread देखें।

उपरोक्त कोड तीन बराबर क्षेत्रों में क्षैतिज स्थान को विभाजित करेगा।

<LinearLayout 
    android:id="@+id/shata" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <TextView 
     android:layout_width="0" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
    <TextView 
     android:layout_width="0" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
    <TextView 
     android:layout_width="0" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/>  
</LinearLayout> 
इस उदाहरण में

तो

  • पहला तत्व दिखता layout_gravity="left" तरह
  • दूसरा तत्व दिखता layout_gravity="center" तरह
  • तीसरा तत्व layout_gravity="right" तरह
+0

थॉमस, आपके नीचे के टेक्स्टव्यू के लेआउट_वेट में डबल-कोट (1 के बाद) गुम है। सिर्फ आपकी जानकारी के लिए। – Alyoshak

0

एक ही रास्ता के लिए लग रहा है क्षैतिज linearlayout woul में तीन बच्चे बनाते हैं डी (परीक्षण किया) जा:

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:weightSum="3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_gravity="center_horizontal" 
      android:layout_height="wrap_content"/> 

      <TextView 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="" /> 
संबंधित मुद्दे