2012-04-11 30 views
13

मैं तीन तत्वों के साथ एक लेआउट है और उसके उन्मुखीकरण 'क्षैतिज'एंड्रॉयड काम नहीं कर रहा weightSum ठीक से

है मैं आकार कि प्रत्येक तत्व मेरी लेआउट में कब्जा करना चाहते हैं।

पहले तत्व को कुल स्थान का 40% लेना चाहिए तो दूसरा तत्व 5% पर कब्जा कर लेना चाहिए और तीसरा व्यक्ति शेष 55% स्थान पर कब्जा कर लेना चाहिए।

इस तरह मेरे लेआउट देखो: 'TextView' में

<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="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="100" > 

    <TextView 
     android:id="@+id/outletname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="40" 
     android:text="@string/outlet_name2" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:text=": " /> 

    <TextView 
     android:id="@+id/outletnamevalue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="55" 
     android:text="abcdefttt" /> 
</LinearLayout> 
</LinearLayout> 

टेक्स्ट डायनामिक बदल जाएगा, तो मैं विचारों में सामग्री पर ध्यान दिए बिना तत्वों के लिए निरंतर अंतरिक्ष रखना चाहते हैं ...

उत्तर

43

में आदेश वजन प्रभावी, आप भी0dp करने के लिए अपने TextViews की चौड़ाई निर्धारित करने की आवश्यकता चाहिए बनाने के लिए।

android:layout_width="0dp" 
+10

जवाब पूरा करने के लिए, एक समझना चाहिए कि वजन केवल/शेष/अंतरिक्ष, यानि कि अंतरिक्ष एक बार layout_width मूल्य लागू किया गया था छोड़ दिया करने के लिए लागू होता है। – njzk2

+0

इसने मेरा दिन बनाया: डी कितने सालों के लिए ... एलओएल। तो यह समस्या पर एकमात्र जवाब है। UPVOTEEEEEEE – jace

0

मैं इस सूत्र का उपयोग:

layout_weight=(weightSum-PercentageYouWant*weightSum)/(TotalElements-1) 

पहला तत्व कुल स्थान का 40% ले तो दूसरा तत्व 5% ले लेना चाहिए चाहिए और तीसरा शेष 55% अंतरिक्ष ले लेना चाहिए।

1 तत्व:

(100-0.4*100)/(3-1)="30" 

2 तत्व:

(100-0.05*100)/(3-1)="47.5" 

3 तत्व:

(100-0.55*100)/(3-1)="22.5" 

तो 40% + 5% + 55% = 100%

layout_weight="30" + layout_weight="47.5" + layout_weight="22.5" = weightSum="100" 

इस तरह मैं इसे समझता हूं।

यहाँ एक उदाहरण है (बटन ग्राफिक संदर्भ के लिए बस कर रहे हैं)

<?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" 
    android:weightSum="120" > 

    <!-- WeightSum=120 and I will add 4 elements, so TotalElements=4 --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="36" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 10%, so (120-10%*120)/(4-1)=36 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="10%" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="32" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 20%, so (120-20%*120)/(4-1)=32 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="20%" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="28" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 30%, so (120-30%*120)/(4-1)=28 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="30%" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="24" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 40%, so (120-40%*120)/(4-1)=24 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="40%" /> 
    </LinearLayout> 

</LinearLayout> 
संबंधित मुद्दे