📜  androidx cardview - Java (1)

📅  最后修改于: 2023-12-03 14:59:16.970000             🧑  作者: Mango

AndroidX CardView - Java

AndroidX CardView is a support library that provides a simple and easy way to implement card-based layouts in Android applications. It is part of the AndroidX library, which is an updated version of the original Android Support Library.

Getting Started

To use AndroidX CardView in your project, you need to do the following steps:

  1. Add the dependency to your app's build.gradle file:
dependencies {
    implementation 'androidx.cardview:cardview:1.0.0'
}
  1. Sync the project with Gradle files.
Basic Usage

To use CardView, you need to add the CardView widget to your layout file and then add other widgets inside the CardView to create a card-based layout.

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/image" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textSize="24sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Description"
            android:textSize="16sp" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

In the above example, we have used CardView to create a basic card-based layout with an image, title, and description.

Customization

CardView provides several attributes that allow you to customize the appearance and behavior of the card. Here are some commonly used attributes:

  • app:cardCornerRadius: Sets the corner radius of the card.
  • app:cardElevation: Sets the elevation of the card, which creates a shadow effect.
  • app:cardUseCompatPadding: Enables or disables compatibility padding to support older versions of Android.
  • app:contentPadding: Sets the padding inside the card.
  • app:cardBackgroundColor: Sets the background color of the card.
  • app:cardPreventCornerOverlap: Allows the card to draw over its padding instead of drawing on top of it.
<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true"
    app:contentPadding="10dp"
    app:cardBackgroundColor="@color/card_background_color"
    app:cardPreventCornerOverlap="false">

    ...

</androidx.cardview.widget.CardView>
Conclusion

In conclusion, AndroidX CardView is a simple and easy-to-use library that makes creating card-based layouts in Android applications a breeze. By customizing the appearance and behavior of your cards with the provided attributes, you can create a unique and visually appealing user interface.