कार्ड पर आधारित लेआउट बनाना

Compose को आज़माएं
Jetpack Compose, Android के लिए सुझाई गई यूज़र इंटरफ़ेस (यूआई) टूलकिट है. Compose में लेआउट इस्तेमाल करने का तरीका जानें.

ऐप्लिकेशन को अक्सर एक जैसे स्टाइल वाले कंटेनर में डेटा दिखाने की ज़रूरत होती है. जैसे, ऐसे कंटेनर जिनमें सूची में मौजूद आइटम की जानकारी होती है. सिस्टम आपको CardView एपीआई उपलब्ध कराता है, ताकि आप कार्ड में जानकारी दिखा सकें. ये कार्ड, पूरे प्लैटफ़ॉर्म पर एक जैसे दिखते हैं. उदाहरण के लिए, कार्ड में उनके व्यू ग्रुप के ऊपर डिफ़ॉल्ट एलिवेशन होता है. इसलिए, सिस्टम उनके नीचे शैडो बनाता है. कार्ड की मदद से, व्यू के ग्रुप को एक साथ रखा जा सकता है. साथ ही, कंटेनर के लिए एक जैसा स्टाइल उपलब्ध कराया जा सकता है.

कार्ड पर आधारित ऐप्लिकेशन के यूज़र इंटरफ़ेस (यूआई) की झलक दिखाने वाली इमेज
पहली इमेज. कार्ड पर आधारित ऐप्लिकेशन का यूज़र इंटरफ़ेस (यूआई).

डिपेंडेंसी जोड़ना

CardView विजेट, AndroidX का हिस्सा है. अपने प्रोजेक्ट में इसका इस्तेमाल करने के लिए, अपने ऐप्लिकेशन मॉड्यूल की build.gradle फ़ाइल में यह डिपेंडेंसी जोड़ें:

ग्रूवी

dependencies {
    implementation "androidx.cardview:cardview:1.0.0"
}

Kotlin

dependencies {
    implementation("androidx.cardview:cardview:1.0.0")
}

कार्ड बनाना

CardView का इस्तेमाल करने के लिए, इसे अपनी लेआउट फ़ाइल में जोड़ें. इसे व्यू ग्रुप के तौर पर इस्तेमाल करें, ताकि अन्य व्यू शामिल किए जा सकें. यहां दिए गए उदाहरण में, CardView में एक ImageView और कुछ TextViews शामिल हैं. इनका इस्तेमाल, उपयोगकर्ता को कुछ जानकारी दिखाने के लिए किया जाता है:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="16dp"
    android:background="#E0F7FA"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:padding="4dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/header_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/logo"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/title"
                style="@style/TextAppearance.MaterialComponents.Headline3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="I'm a title"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/header_image" />

            <TextView
                android:id="@+id/subhead"
                style="@style/TextAppearance.MaterialComponents.Subtitle2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="I'm a subhead"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/title" />

            <TextView
                android:id="@+id/body"
                style="@style/TextAppearance.MaterialComponents.Body1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="I'm a supporting text. Very Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/subhead" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

पिछले कोड स्निपेट से, यहां दिए गए उदाहरण जैसा कुछ मिलता है. इसके लिए, यह ज़रूरी है कि आपने Android लोगो की वही इमेज इस्तेमाल की हो:

एक कार्ड दिखाने वाली इमेज
दूसरी इमेज. CardView पर आधारित लेआउट का एक बुनियादी उदाहरण.

इस उदाहरण में, कार्ड को डिफ़ॉल्ट एलिवेशन के साथ स्क्रीन पर दिखाया गया है. इस वजह से, सिस्टम इसके नीचे एक शैडो दिखाता है. card_view:cardElevation एट्रिब्यूट की मदद से, किसी कार्ड के लिए कस्टम एलिवेशन सेट किया जा सकता है. ज़्यादा ऊंचाई पर मौजूद कार्ड की परछाई ज़्यादा गहरी होती है. वहीं, कम ऊंचाई पर मौजूद कार्ड की परछाई हल्की होती है. CardView, Android 5.0 (एपीआई लेवल 21) और इसके बाद के वर्शन पर, रीयल एलिवेशन और डाइनैमिक शैडो का इस्तेमाल करता है.

CardView विजेट के लुक को पसंद के मुताबिक बनाने के लिए, इन प्रॉपर्टी का इस्तेमाल करें:

  • अपने लेआउट में कोने का रेडियस सेट करने के लिए, card_view:cardCornerRadius एट्रिब्यूट का इस्तेमाल करें.
  • अपने कोड में कॉर्नर रेडियस सेट करने के लिए, CardView.setRadius तरीके का इस्तेमाल करें.
  • किसी कार्ड के बैकग्राउंड का रंग सेट करने के लिए, card_view:cardBackgroundColor एट्रिब्यूट का इस्तेमाल करें.