2011-12-05 11 views
6

रैखिक लेआउट के बारे में जानने के लिए मैं this tutorial का अनुसरण कर रहा हूं।लिनियरलाइट विशेषता "लेआउट_वेट" क्यों करती है जो मुझे लगता है कि इसके विपरीत क्या करना चाहिए?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView 
      android:text="red" 
      android:gravity="center_horizontal" 
      android:background="#aa0000" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="green" 
      android:gravity="center_horizontal" 
      android:background="#00aa00" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="blue" 
      android:gravity="center_horizontal" 
      android:background="#0000aa" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="yellow" 
      android:gravity="center_horizontal" 
      android:background="#aaaa00" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <TextView 
     android:text="row one" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row two" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row three" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row four" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    </LinearLayout> 

</LinearLayout> 

कौन इस का उत्पादन:

linear layout example

मैं शीर्ष पर रंग सलाखों (लेआउट के पहले बच्चे) बनाने की कोशिश कर के साथ प्रयोग किया गया था खड़ी दो बार के रूप यहाँ मुख्य लेआउट फ़ाइल है पहले बच्चे के लेआउट वजन बनाकर तल पर क्षैतिज पंक्तियों (अंतिम संतान) के रूप में बड़ा दो बार दूसरे बच्चे की:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="2"> 
     <TextView 
      android:text="red" 
      android:gravity="center_horizontal" 
      android:background="#aa0000" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="green" 
      android:gravity="center_horizontal" 
      android:background="#00aa00" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="blue" 
      android:gravity="center_horizontal" 
      android:background="#0000aa" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="yellow" 
      android:gravity="center_horizontal" 
      android:background="#aaaa00" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <TextView 
     android:text="row one" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row two" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row three" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row four" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    </LinearLayout> 

</LinearLayout> 

लेकिन यह मेरे अपेक्षित परिणाम के विपरीत करता है: यह वास्तव में रंगीन सलाखों को लगभग आधा तक लंबवत बनाता है।

एंड्रॉयड डेवलपर दस्तावेज़ निर्दिष्ट करें कि

any remaining space in the view group is assigned to children in the proportion of their declared weight 

क्यों यह प्रतीत होता है उलटा तरह सही है? किसी भी शेष अंतरिक्ष पर

+3

यह वास्तव में विपरीत है। जिसे आप बड़ा होना चाहते हैं उसे वजन के रूप में कम संख्या दी जानी चाहिए। – Hiral

+0

@ हायरल इसे वापस करने की देखभाल करते हैं? क्या यह हमेशा सत्य है या केवल जब आप 'fill_parent' का उपयोग करते हैं? मुझे पता है कि जब आप प्रतिस्पर्धी विचारों को – Craigy

+0

@ क्रैगी के साथ शुरू करने के लिए प्रतिस्पर्धात्मक विचार देते हैं तो वास्तव में सच है: वास्तव में मुझे नहीं पता कि यह वास्तव में विभिन्न मामलों के साथ क्या व्यवहार करता है। लेकिन लगभग हर बार, जैसा कि मैंने वर्णन किया है, यह विपरीत कार्य करता है। लेकिन हाँ, कभी-कभी यह अलग-अलग काम करता है। मुझे उस समय कारण पता नहीं था। लेकिन अब मुझे लगता है कि, fill_parent आपके व्यवहार को प्रभावित कर सकता है जैसा आपने कहा था। – Hiral

उत्तर

10

जोर ...

मुझे लगता है कि ऊंचाई और लेआउट वजन के लिए fill_parent का उपयोग कर के बीच एक संघर्ष है। LinearLayout एस दोनों के लिए android:layout_height="0dp" सेट करने का प्रयास करें।

5

लेआउट_वेट आमतौर पर लेआउट_विड्थ या लेआउट_हेइट के साथ wrap_content पर सेट किया जाता है। इसे आज़माएं, आप देखेंगे कि यह उस लेआउट आकार को बढ़ाता है जिसके आधार पर उच्च वजन होता है।

उदाहरण

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2"> 
     <TextView 
      android:text="red" 
      android:gravity="center_horizontal" 
      android:background="#aa0000" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 

    </LinearLayout> 

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
    <TextView 
     android:text="row one" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

    </LinearLayout> 

</LinearLayout> 
3
<?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" android:weightSum="3"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#aa0000" 
      android:gravity="center_horizontal" 
      android:text="red" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#00aa00" 
      android:gravity="center_horizontal" 
      android:text="green" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000aa" 
      android:gravity="center_horizontal" 
      android:text="blue" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#aaaa00" 
      android:gravity="center_horizontal" 
      android:text="yellow" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="row one" 
      android:textSize="15pt" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="row two" 
      android:textSize="15pt" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="row three" 
      android:textSize="15pt" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="row four" 
      android:textSize="15pt" /> 
    </LinearLayout> 

</LinearLayout> 

enter image description here

एंड्रॉयड निर्दिष्ट करें: weightSum माता पिता लेआउट के लिए है और यह भी एंड्रॉयड: layout_height = "0dp" बच्चे लेआउट के लिए। कृपया इसे आजमाएं। उम्मीद है कि यह मदद करेगा।

+1

उत्तर उपयोगी था। लेकिन ऐसा लगता है कि आपको एंड्रॉइड की आवश्यकता नहीं है: weightSum निर्दिष्ट है। एंड्रॉइड प्रलेखन के अनुसार: "यदि निर्दिष्ट नहीं है, तो सभी बच्चों के लेआउट_वेट को जोड़कर योग की गणना की जाती है।" – Aishwar

संबंधित मुद्दे

 संबंधित मुद्दे