2012-03-19 10 views
5

को कवर करने के लिए बटन क्यों फैलते हैं मैं 3 बटन गठबंधन (बाएं, केंद्र, दाएं) प्राप्त करने का प्रयास कर रहा हूं। मुझे पता है कि यह शायद गुरुत्वाकर्षण के साथ किया जा सकता है लेकिन मैं लेआउट_वेट अवधारणा को समझने की कोशिश कर रहा हूं। मैं इस कोड का उपयोग कर रहा हूं:सभी लेआउट_वेट आवंटित

<LinearLayout 
id=.. 
layout_width = "wrap_content" 
layout_height = "wrap_content" 
orientation="Horizonta" 
> 
<Button 
    id=1 
    layout_width = "wrap_content" 
    layout_height = "wrap_content" 
    layout_weight = "1" 
    text= "text" /> 

<Button 
    id=2 
    layout_width = "wrap_content" 
    layout_height = "wrap_content" 
    layout_weight = "1" 
    text= "text" /> 

<Button 
    id=3 
    layout_width = "wrap_content" 
    layout_height = "wrap_content" 
    layout_weight = "1" 
    text= "text" /> 
</LinearLayout> 

हालांकि, मुझे नीचे दी गई छवि का शीर्ष भाग मिल रहा है। क्या सही है। प्रश्न यह है कि जब मैं wrap_content का उपयोग कर रहा हूं, तो आवंटित सभी क्षेत्र को कवर करने के लिए बटन क्यों बढ़ाया जाता है? इसे BOTTOM छवि की तरह कैसे बना सकता है?

enter image description here

आप

+0

धन्यवाद! मैं इस पर दो घंटों के लिए काम कर रहा हूं! –

उत्तर

5

layout_weight दर्शाने के लिए स्क्रीन पर अतिरिक्त मुक्त अंतरिक्ष विगेट्स के मध्य विभाजित किया जाना चाहिए किया जाता है। इस मामले में हमारे पास एक ही लेआउट_वेट के साथ तीन बटन हैं और इसलिए सभी खाली स्थान समाप्त होने तक बटन समान रूप से बढ़ेगा।

मूल आकार बटन और संरेखण प्राप्त करने के लिए कुछ और प्रयास करना चाहिए जो हम चाहते हैं (बाएं - केंद्र - दाएं)। एक समस्या यह है कि लेआउट_ग्रैविटी पैरामीटर का सम्मान करने पर क्षैतिज लीनियरलाउट पूरी तरह से सहयोगी नहीं है। हम लेआउट_ग्रैविटी पैरामीटर का उपयोग करने में सक्षम होंगे जो लंबवत संरेखण को प्रभावित करते हैं लेकिन क्षैतिज को लीनियरलायआउट द्वारा अनदेखा किया जाता है, एक और मृत अंत का आह्वान करता है।

एक समाधान बटन के बीच में कुछ डालना है जो उन्हें स्क्रीन में सही जगह पर धक्का देता है। अंतरिक्ष दृश्य एपीआई 14 (आइस क्रीम सैंडविच) में पेश किया गया था और इसके लिए उपयोग करने के लिए उपयुक्त है।

<?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="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Space 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight = "1" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Space 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight = "1" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 

अंतरिक्ष बार देखा भी उपकरणों है कि पहले चलाने एपीआई स्तर पर काम करने की डिजाइन पाने के लिए खाली TextViews के एक जोड़े के साथ प्रतिस्थापित किया जा सकता है।

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:text="" />  
+0

बिल्कुल सही जवाब। धन्यवाद – Snake

2

जब वजन प्रयोग किया जाता है धन्यवाद, layout_width ommited जाता है और केवल वजन एल्गोरिथ्म प्रयोग किया जाता है और उसके बाद तत्व खिंचाव।

1

आपके बटन स्पेस भरते हैं क्योंकि आप layout_weight="1" का उपयोग करते हैं जो बटन को अतिरिक्त स्थान उपलब्ध कराने के लिए कहता है, क्योंकि layout_weight विशेषता सभी बटनों के लिए 1 है, वे एक ही स्थान ले लेंगे और एक ही आकार के होंगे। आप layout_weight का उपयोग कर उस नीचे लेआउट को प्राप्त नहीं कर सकते हैं, उन्हें layout_gravity का उपयोग करने के लिए उपयोग करें (यहां इस What does android:layout_weight mean? के बारे में एक और प्रश्न है)।

आप bellow छवि यह कोशिश करना चाहते हैं:

<LinearLayout 
id=.. 
layout_width = "wrap_content" 
layout_height = "wrap_content" 
orientation="Horizonta" 
> 
<Button 
    id=1 
    layout_width = "wrap_content" 
    layout_height = "wrap_content" 
    android:layout_gravity="left" 
    text= "text" /> 

<Button 
    id=2 
    layout_width = "wrap_content" 
    layout_height = "wrap_content" 
    android:layout_gravity="center_horizontal" 
    text= "text" /> 

<Button 
    id=3 
    layout_width = "wrap_content" 
    layout_height = "wrap_content" 
    android:layout_gravity="right" 
    text= "text" /> 
</LinearLayout> 
संबंधित मुद्दे