2011-03-28 15 views
26

मैं स्क्रीन के बीच में तीन बटन प्रदर्शित करना चाहता हूं, और तीन बटन सभी एक ही चौड़ाई वाले हैं, हालांकि उनके पास अलग-अलग लंबाई के टेक्स्ट लेबल होंगे।समान चौड़ाई वाले बटन कैसे बनाते हैं?

विभिन्न लंबाई के टेक्स्ट लेबल के साथ बस तीन बटन जोड़ना विभिन्न चौड़ाई के बटन उत्पन्न करता है। -

1 layout_weight और layout_width सभी बटन पर 0dip करने के लिए उन्हें समान रूप से फैलाने के लिए पूरे स्क्रीन को भरने के लिए कारण बनता है की स्थापना
default button width wraps contents

:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="JKLM" /> 
</LinearLayout>

डिफ़ॉल्ट बटन चौड़ाई सामग्री लपेटता चौड़ाई। जो मैं चाहता हूं, उसके लिए ऐसे बटन बहुत बड़े हैं, खासकर बड़ी स्क्रीन पर। ,

माता पिता LinearLayout में weightSum के लिए अलग मान स्थापना पूरे स्क्रीन भरने से बटन को रोकने के लिए इस्तेमाल किया जा सकता -
layout weight 1 buttons fill screen width

:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout>

लेआउट वजन 1 बटन स्क्रीन चौड़ाई को भरने लेकिन मुझे नहीं लगता कि यह वह रास्ता है जिसे मैं लेना चाहता हूं, क्योंकि मैं नहीं चाहता कि बटन बड़े स्क्रीन उपकरणों पर स्क्रीन के बड़े हिस्से पर कब्जा कर लें। वज़नसम का उपयोग करके स्पष्टीकरण के लिए, उदाहरण के लिए, मैं तीन बटनों को सामूहिक रूप से आधा स्क्रीन चौड़ाई पर कब्जा करने के लिए सेट कर सकता हूं, जो छोटी स्क्रीन पर ठीक दिख सकता है, लेकिन बड़ी स्क्रीन पर, बटन अभी भी आधा स्क्रीन चौड़ाई पर कब्जा कर लेते हैं, और बटन जो भी मैं चाहता हूं उससे काफी बड़ा होगा। शायद अंतिम समाधान के लिए अलग-अलग स्क्रीन के लिए अलग-अलग लेआउट फाइलें होंगी, लेकिन मैं इस पथ को नीचे नहीं जाना चाहूंगा।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center" 
    android:weightSum="5"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout>

वजन राशि 5 छोटे परदे:
weight sum 5 small screen

वजन राशि 5 बड़ी स्क्रीन:
weight sum 5 large screen

-

मैं भी TableLayout साथ बहुत सी बातें करने की कोशिश की, लेकिन 'नहीं था केवल LinearLayout का उपयोग करने से बेहतर कुछ नहीं मिलता है।

ग्रिड व्यू उपयोग करने के लिए अतिरिक्त बेकार है, और मैंने अभी तक यह कोशिश नहीं की है।

तो, कोई समान चौड़ाई वाले बटन कैसे बनाता है, वहीं, जहां वे सबसे लंबे लेबल वाले बटन की सामग्री को फिट करने के लिए आवश्यक हैं?

किसी भी सलाह की सराहना की जाती है।

+1

कैसे एक कस्टम लेआउट लिखने के बारे में? – bigstones

उत्तर

5

शायद अंतिम समाधान बस के लिए होगा (मैं खोज और इस प्रश्न पूछा और कई बार जवाब मिल जाए, लेकिन जवाब मैं मैं क्या हासिल करने की कोशिश कर रहा हूँ हल हो पाया में से कोई भी। किया था) विभिन्न स्क्रीन के लिए अलग-अलग लेआउट फाइलें, लेकिन मैं इस पथ को नीचे नहीं जाना चाहूंगा।

कई प्रोग्रामर इस तरह की स्थितियों को संभालने के लिए res/layout/ और res/layout-large/ का उपयोग करेंगे। तीन बटनों के सीमित मामले में, आपके पास विकल्प हो सकते हैं, लेकिन आमतौर पर उपयोगकर्ता इंटरफेस काफी सरल नहीं होते हैं।

तो, एक समान चौड़ाई वाले बटन कैसे बनाते हैं, वहीं, जहां वे सबसे लंबे लेबल के साथ बटन की सामग्री को फिट करने के लिए आवश्यक हैं?

अपनी "प्राथमिकता" [एसआईसी] आवश्यकता को पूरा करने के लिए, आपको इसके लिए एक कस्टम लेआउट क्लास बनाना होगा। डैशबोर्ड पैटर्न के लिए Here is one related to it, कि आप एक शुरुआती बिंदु के रूप में उपयोग कर सकते हैं।

+0

Arg। [यह स्वीकार करते हुए कि एंड्रॉइड वह नहीं करेगा जो मैं चाहता हूं। ऐसा लगता है कि बटनों को बराबर ऊंचाई रखने के लिए बटन सेट करने के लिए एक सुविधा मौजूद है, लेकिन समान चौड़ाई नहीं है (वजन का उपयोग करके w/o)।] –

-1

इस सूत्र पढ़ना, तो कुछ परीक्षण और त्रुटि कर और पाया है कि android:layout_width="180px" एक स्वीकृत पैरामीटर है। अब कहा गया है, मैं बस इस पर ठोकर खाई, अपने तीन बटन परिदृश्य को हल करने के लिए इसका उपयोग करने की कोशिश नहीं की।

यह अच्छी तरह से हो सकता है कि मूल रूप से पोस्ट होने के बाद से चीजें बदली गईं। हालांकि मैंने संस्करण 1.5 के निर्माण के दौरान यह कोशिश की। यही कारण है कि काफी पुरानी है ... :-)

यहाँ पूरा main.xml है: पहले से

<?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"> 

    <TextView 
     android:id="@+id/dateText" 
     android:text="\n\nClick for Date\n\n" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="180px" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Get the Time" 
     android:onClick="showNewDate" /> 

</LinearLayout> 
+0

1. आपको 'dp' और' px' का उपयोग नहीं करना चाहिए। 2. एक विशिष्ट डीपी राशि में चौड़ाई निर्धारित करने से यह गारंटी नहीं मिलती है कि बटन के अंदर का पाठ फिट होगा (यह तब पाठ पर निर्भर करता है)। –

3

यदि आप जानते हैं आपके व्यापक बटन पाठ आपको लगता है कि करने के लिए अपने बटन सेट करने के लिए android:ems विशेषता का उपयोग कर सकते हैं हो सकता है क्या चौड़ाई।

<Button 
    android:id="@+id/my_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minEms="5" 
    android:text="@string/my_button" /> 

यह सही नहीं है, लेकिन यह सबसे आसान तरीका है कि मैं बेहद लेआउट के साथ लगभग नगण्य बिना इस रंग-रूप को प्राप्त करने के पाया गया है।

0

मेरी तरफ से सिर्फ एक अतिरिक्त नोट!

यदि आपको रनटाइम पर जोड़े गए बटन (या कोड के साधारण परिवर्तन वाले किसी भी अन्य दृश्य) के लिए चौड़ाई (या ऊंचाई) सेट करने की आवश्यकता होगी, तो आप नीचे दिए गए कोड का उपयोग कर सकते हैं (यह अभिविन्यास पर निर्भर नहीं है):

public void AlignButtons(){ 
    llModules = (LinearLayout) findViewById(R.id.llModules); 

    ViewTreeObserver viewTree = llModules.getViewTreeObserver(); 
    viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 

      int childcount = llModules.getChildCount(); 
      int maxWidth = 0; //int maxHeight = 0; 

      String fontPath = "fonts/Isabella-Decor.ttf"; 
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 

      for (int i = 0; i < childcount; i++) { 
       LinearLayout v = (LinearLayout)llModules.getChildAt(i); 
       View vv = v.getChildAt(0); 
       if (vv instanceof Button) { 
        int width = vv.getMeasuredWidth(); 
        maxWidth = (maxWidth > width) ? maxWidth : width; 
        //int height = vv.getMeasuredHeight(); 
        //maxHeight = (maxHeight > height) ? maxHeight : height; 
       } 
      } 
      for (int i = 0; i < childcount; i++) { 
       LinearLayout v = (LinearLayout)llModules.getChildAt(i); 
       View vv = v.getChildAt(0); 
       if (vv instanceof Button) { 
        LayoutParams params = ((Button) vv).getLayoutParams(); 
        params.width = maxWidth; 
        //params.height = maxHeight; 
        ((Button) vv).setLayoutParams(params); 

        // Applying font 
        ((Button) vv).setTypeface(tf); 
       } 
       vv = v.getChildAt(1); 
       if (vv instanceof TextView) { 
        // Applying font 
        ((TextView) vv).setTypeface(tf); 
       } 
      } 

      return true; 
     } 
    }); 
} 

यहाँ, मेरे मामले में:

llModule - कुछ हम क्या संरेखित करने के लिए (v.getChildAt(0); की जरूरत के साथ एक और लेआउट युक्त लेआउट) और अन्य (v.getChildAt(1);)।

LinearLayout v = (LinearLayout) llModules.getChildAt (i); - संरेखित करने के लिए बटन का लेआउट प्राप्त करें।

अन्य भागों को समझने के लिए स्पष्ट हैं।

इस कोड में दो चक्र काफी समान हैं, लेकिन मैं अभी भी कल्पना नहीं कर सकता कि उन्हें कैसे संयोजित किया जाए (निष्पादन समय को तेज करने के लिए)।

0

इस उचित कोड का उपयोग करें इससे आपकी मदद मिलेगी।

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

<Button 
    android:id="@+id/button1" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 
<Button 
    android:id="@+id/button3" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 

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