Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में fill_parent (match_parent) और wrap_content में क्या अंतर है?

एंड्रॉइड Layout में match_parent vs wrap_content — पूरा अंतर समझाया गया

ये दो सबसे मौलिक लेआउट चौड़ाई/ऊंचाई मोड हैं जो हर एंड्रॉइड डेवलपर को पता होने चाहिए। नोट: fill_parent API लेवल 8 (Android 2.2) में डिप्रिकेटेड हो गया था — इसका आधुनिक नाम match_parent है। दोनों समान व्यवहार करते हैं।

त्वरित तुलना

विशेषताmatch_parent (पूर्व fill_parent)wrap_content
अर्थपैरेंट व्यू जितना बड़ा बनेअपने कंटेंट जितना बड़ा बने
चौड़ाई/ऊंचाईपैरेंट के उपलब्ध स्थान को भरता हैकंटेंट (टेक्स्ट, इमेज, चाइल्ड व्यूज) को फिट करता है
पैरेंट पर निर्भरताहां — पैरेंट का आकार जानना जरूरीनहीं — केवल अपने कंटेंट पर निर्भर
सामान्य उपयोगफुल-स्क्रीन कंटेनर, ListView/RecyclerView, बैकग्राउंडबटन, टेक्स्ट व्यू, इमेज, छोटे कंपोनेंट्स
प्रदर्शनथोड़ा अधिक मेजरमेंट पासतेज़ — केवल अपना कंटेंट मापता है

दृश्य उदाहरण

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- match_parent: पूरी चौड़ाई लेगा -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="MATCH_PARENT (पूरी चौड़ाई)"
        android:background="#2196F3"
        android:textColor="#FFF" />

    <Space android:layout_width="match_parent" android:layout_height="16dp" />

    <!-- wrap_content: केवल टेक्स्ट जितना चौड़ा -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WRAP_CONTENT"
        android:background="#4CAF50"
        android:textColor="#FFF" />

    <Space android:layout_width="match_parent" android:layout_height="16dp" />

    <!-- नेस्टेड: पैरेंट wrap_content, चाइल्ड match_parent -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#FFEB3B"
        android:padding="16dp">

        <TextView
            android:layout_width="match_parent"  <!-- पैरेंट (wrap_content) जितना -->
            android:layout_height="wrap_content"
            android:text="पैरेंट wrap_content है, इसलिए मैं भी उतना ही चौड़ा हूं"
            android:textColor="#000"
            android:background="#FFF9C4"
            android:padding="8dp" />

    </LinearLayout>

</LinearLayout>

व्यवहार विवरण

1. match_parent (fill_parent)

  • पैरेंट के **उपलब्ध स्थान** को पूरी तरह भरता है (पैरेंट के पैडिंग घटाकर)।
  • अगर पैरेंट wrap_content है, तो चाइल्ड का match_parent पैरेंट के कंटेंट-निर्धारित आकार तक सीमित होता है।
  • पूर्ण-स्क्रीन लेआउट्स, RecyclerView, फ्रैगमेंट कंटेनर के लिए मानक।

2. wrap_content

  • व्यू के **आंतरिक कंटेंट** (टेक्स्ट, ड्रॉएबल, चाइल्ड व्यूज) को फिट करने के लिए न्यूनतम आकार लेता है।
  • पैरेंट के आकार पर निर्भर नहीं करता — स्वतंत्र।
  • बटन, लेबल, आइकन, कार्ड व्यूज के लिए आदर्श।

नेस्टिंग व्यवहार (महत्वपूर्ण!)

<!-- केस 1: पैरेंट match_parent, चाइल्ड wrap_content -->
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView android:layout_width="wrap_content" ... />  <!-- केवल टेक्स्ट जितना -->
</LinearLayout>

<!-- केस 2: पैरेंट wrap_content, चाइल्ड match_parent -->
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TextView android:layout_width="match_parent" ... />  <!-- पैरेंट जितना (जो कंटेंट जितना है) -->
</LinearLayout>

<!-- केस 3: दोनों match_parent -->
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- पूरा स्क्रीन -->
</LinearLayout>

ConstraintLayout में समतुल्य

ConstraintLayout में match_parent/wrap_content सीधे उपयोग नहीं होते। बजाय इसके:

पारंपरिकConstraintLayout समतुल्य
match_parentlayout_width="0dp" + layout_constraintStart_toStartOf="parent" + layout_constraintEnd_toEndOf="parent"
wrap_contentlayout_width="wrap_content" (काम करता है)
वेट/अनुपातlayout_constraintHorizontal_weight + layout_width="0dp"

प्रदर्शन टिप्स

  • wrap_content आमतौर पर तेज़ होता है क्योंकि मेजरमेंट पास सरल होता है।
  • गहरे नेस्टेड लेआउट्स में match_parent चेन कई मेजरमेंट पास ट्रिगर कर सकते हैं।
  • ConstraintLayout फ्लैट हायरार्की के साथ दोनों से बेहतर प्रदर्शन देता है।

सामान्य गलतियाँ

  1. fill_parent उपयोग करना — डिप्रिकेटेड है, match_parent लिखें।
  2. RelativeLayout में match_parent पर भरोसा करना जब पैरेंट wrap_content हो — अप्रत्याशित परिणाम।
  3. ScrollView में match_parent height — ScrollView wrap_content होना चाहिए, चाइल्ड match_parent नहीं (अन्यथा स्क्रॉल नहीं होगा)।
  4. ImageView में wrap_content + scaleType — स्केल टाइप के आधार पर आकार बदल सकता है, adjustViewBounds="true" जोड़ें।

सारांश

  • 🔵 match_parent = "पैरेंट जितना बड़ा बनो" (पूर्व fill_parent)
  • 🟢 wrap_content = "मेरे कंटेंट जितना बड़ा बनो"
  • 🎯 सही चुनाव: कंटेनर/फुल-स्क्रीन → match_parent; कंपोनेंट/कंटेंट-ड्रिवन → wrap_content
  • ⚡ ConstraintLayout में 0dp + कंस्ट्रेंट्स = match_parent
  • 📝 कोड में: ViewGroup.LayoutParams.MATCH_PARENT (-1), WRAP_CONTENT (-2)
  1. डायरेक्टएक्स 11 और डायरेक्टएक्स 12 में क्या अंतर है? डायरेक्टएक्स 11 और डायरेक्टएक्स 12 में क्या अंतर है?

    डायरेक्टएक्स एपीआई का एक संग्रह है, या एप्लिकेशन प्रोग्रामिंग इंटरफेस, जिसका उपयोग सॉफ्टवेयर द्वारा मल्टीमीडिया सामग्री को रेंडर करने और ग्राफिकल हार्डवेयर के साथ संचार करने के लिए किया जाता है। डायरेक्टएक्स का प्रमुख घटक एपीआई, डायरेक्ट3डी, सॉफ्टवेयर और ग्राफिक्स हार्डवेयर के बीच संचार को संभालता ह

  1. Android में gravity और layout_gravity का अंतर Android में gravity और layout_gravity का अंतर

    त्वरित सारांश विशेषताgravitylayout_gravity लागू होता हैव्यू के अंदर कंटेंट परव्यू को पेरेंट के अंदर पोजिशन करता है एनालॉजीबॉक्स के अंदर टेक्स्ट संरेखणकंटेनर में बॉक्स की स्थिति पेरेंट आवश्यकताकोई विशेष नहींपेरेंट LinearLayout/FrameLayout होना चाहिए

  1. Android में onCreate() और onStart() में क्या अंतर है Android में onCreate() और onStart() में क्या अंतर है

    परिचयएक्टिविटी जीवनचक्र में `onCreate()` और `onStart()` दो महत्वपूर्ण कॉलबैक हैं, लेकिन उनके उद्देश्य और समय अलग-अलग हैं।मुख्य अंतरपहलूonCreate()onStart()कब कॉल होता हैएक्टिविटी पहली बार बनने परएक्टिविटी उपयोगकर्ता को दिखाई देने वाली होती हैआवृत्तिजीवनचक्र में एक बारहर बार एक्टिविटी फोरग्राउंड में आ