2011-01-24 14 views
11

में समान रूप से स्थानांतरित बटन मैं एंड्रॉइड पर पोर्ट्रेट व्यू में समान रूप से दूरी वाले 4 बटन बनाने की कोशिश कर रहा हूं।लेआउट

अंतरिक्ष को स्क्रीन आकार के आधार पर ऊपर और नीचे स्केल करना चाहिए, प्रत्येक बटन और सीमाओं के बीच की एक भी मात्रा में स्थान के साथ।

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

क्या यह एंड्रॉइड लेआउट का एक बग है? शायद मैं सिर्फ मुझे है।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
> 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_weight="1" 
android:layout_gravity="center" 
> 
<Button 
android:id="@+id/btn_f" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="btn_f" 
> 
</Button> 
</LinearLayout> 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_weight="1" 
android:layout_gravity="center" 
> 
<Button 
android:id="@+id/btn_b" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="btn_b" 
> 
</Button> 
</LinearLayout> 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_weight="1" 
android:layout_gravity="center" 
> 
<Button 
android:id="@+id/btn_a" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="btn_a" 
> 
</Button> 
</LinearLayout> 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_weight="1" 
android:layout_gravity="center" 
> 
<Button 
android:id="@+id/settings" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Settings" 
> 
</Button> 
</LinearLayout> 
</LinearLayout> 

उत्तर

20

कुछ इस तरह का प्रयास करें:

<?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" 
    > 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:orientation="vertical" 
     android:layout_weight="1" 
     android:gravity="center" 
     > 
     <Button 
      android:id="@+id/btn_f" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="btn_f" 
      /> 
    </LinearLayout> 
    <!-- etc --> 
</LinearLayout> 

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

+1

इस तरह यह काम नहीं करता ... लेकिन यदि आप दोनों लेआउट_ग्रैविटी और गुरुत्वाकर्षण केंद्र के रूप में निर्दिष्ट करते हैं तो यह काम करता है .... निश्चित एंड्रॉइड एक्सएमएल इंटरफेस के लिए POORLY दस्तावेज है .... – Mascarpone

+0

यह एंड्रॉइड होना चाहिए: layout_gravity = "center" एंड्रॉइड नहीं: गुरुत्वाकर्षण = "केंद्र"। एंड्रॉइड का उपयोग करना आवश्यक नहीं है: गुरुत्वाकर्षण = "केंद्र" यहां। गुरुत्वाकर्षण टैग इसकी सामग्री को पोजीशन करने पर प्रभाव डालता है जबकि लेआउट_ग्रैविटी अपने माता-पिता के संबंध में स्थिति को प्रभावित करता है। – wooohoh

1

तुम बस उन्हें माता-पिता LinearLayout में रखने के बजाय एक और LinearLayout में सभी बटन लपेटकर कर रहे हैं।

उन सभी तत्काल LinearLayout को हटा दें ताकि android:layout_weight प्रभावी हो।


बटन में खींच और रिक्ति

  • उपयोग android:layout_width="fill_parent" खुद को
  • रिक्ति के लिए मार्जिन निर्दिष्ट लक्ष्य को हासिल करने
+0

यह बस बटन खींचता है ... मैं चाहता हूं कि बटन उनके चारों ओर की जगह के साथ छोटे हों ... प्रत्येक आयत के केंद्र में – Mascarpone

+2

मेरा अपडेट देखें कृपया – Aliostad

+2

मार्जिन निर्दिष्ट करना मैं बचाना चाहता हूं ... आप एक आयाम डालने की आवश्यकता है और भले ही यह डुबकी में है, अंतिम प्रभाव प्रत्येक प्रदर्शन पर समान नहीं है ... क्या वजन के साथ कोई स्वचालित तरीका नहीं है? – Mascarpone

3

इस प्रयास करें:

<LinearLayout   
    android:layout_width="wrap_content"   
    android:layout_height="0dp"   
    android:orientation="horizontal"   
    android:weightSum="1"   
    android:gravity="center"> 
    <Button    
     android:id="@+id/btn_f"  
     android:layout_width="0dp" 
     android:layout_weight="0.5" 
     android:layout_height="wrap_content"  
     android:text="btn_f" />  
</LinearLayout> 

इस स्क्रीन की लंबाई का केवल आधा कब्जा करने के लिए बटन सेट हो जाएगा।

+1

हालांकि यह बटन खींच देगा। – hadi

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