Android: LinearLayout के अंदर सभी तत्वों को समान आकार कैसे बनाएं?


80

मैं एक वीडियो शीर्षक और टैग प्रदर्शित करने के लिए एक संवाद बनाना चाहूंगा। पाठ के नीचे मैं बटन जोड़ना, संपादित करना और हटाना और इन तत्वों को समान आकार देना चाहूंगा। क्या किसी को पता है कि LinearView के अंदर तत्वों को बनाने के लिए .xml लेआउट फ़ाइल को कैसे संशोधित किया जाए?

वर्तमान लेआउट फ़ाइल इस तरह दिखती है:

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

    <LinearLayout 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:orientation="vertical">

          <TextView 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:id="@+id/txtTitle" android:text="[Title]" >
          </TextView>

          <TextView 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:id="@+id/txtTags"            
              android:text="[Tags]" >
          </TextView>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

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

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

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

    </LinearLayout>

</LinearLayout>

मैं सराहना करता हूं कि अगर कोई भी पेस्ट की गई फ़ाइल सामग्री को संशोधित करके समाधान दिखा सकता है।

धन्यवाद!

जवाबों:


173

का प्रयोग करें android:layout_width="0px"और android:layout_weight="1"तीन Buttonएस पर। यह कहता है कि बटन को 0 पिक्सेल से अधिक नहीं लेना चाहिए, लेकिन उन तीनों को उनके बीच किसी भी अतिरिक्त स्थान को विभाजित करना चाहिए। आपको वह दृश्य प्रभाव देना चाहिए जो आप चाहते हैं।


4
यदि आप वास्तव में चाहते थे कि आप वैकल्पिक रूप से Android के साथ एक TableLayout का उपयोग कर सकते थे: stretchColumns = "0,1,2"।
जेरेमी लोगान

बहुत बढ़िया ... तो इसका मतलब है, अगर हम 0px के रूप में चौड़ाई डालते हैं, तो वजन सेटिंग्स को ओवरराइड करेगा? ऐसा क्यों है?
noob

यदि आपके बटन एक में हैं तो यह काम नहीं करता है GridLayout। इस मामले में वे वास्तव में 0pxव्यापक हो जाते हैं । शायद GridLayoutस्वीकार नहीं करता है layout_weight?
ज़ेल्फिर कल्टस्टाहल

@Zelphir: GridLayoutनिश्चित रूप से उपयोग नहीं करता है layout_weight। इसका उपयोग केवल LinearLayoutऔर शायद कुछ उपवर्गों द्वारा किया जाता है LinearLayout
कॉमन्सवेयर

33

एक और तरीका है बनाने के लिए android:layout_width="fill_parent"और android:layout_weight="1"यह भी ठीक काम करेगा !!!


19
आप वास्तव में इस उत्तर के बारे में उत्साहित थे। :-) उत्साह से प्यार करो!
ओह डैनी बॉय

कॉमन्सवेयर के समाधान ने मेरे लिए काम नहीं किया, लेकिन यह किया! :) मैंने सुना है कि आप "fill_parent" पर इसे पसंद करते हैं, लेकिन दोनों काम करते हैं।
कोडप्लेब

LinearLayout उन तत्वों की मूल भी होना आवश्यक है layout_width = "match_parent"
पटपूम

19

उपयोग LinearLayout अपने वांछित साथ weightSum और समान के साथ तत्वों को बनाने layout_weight । यहाँ एक उदाहरण है ...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_share_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_search_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_event_note_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_brush_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_menu_white_36dp"/>
</LinearLayout>

तो, सभी तत्वों का वजन योग 5 है। यहाँ स्क्रीनशॉट है ...

यहाँ छवि विवरण दर्ज करें

ध्यान दें, Google सामग्री डिज़ाइन आइकन का उपयोग किया जाता है। आशा है कि यह उपयोगी है।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.