728x90

CardView를 사용하는 가장 중요한 이유는 깔끔하고 이뻐서다!

안드로이드 CardView 는 support.v7.widget 에 속한 라이브러리로 SDK 21버전부터 CardView가 추가되었다.

Android RecyclerView 를 사용하면서 CardView 를 같이 사용 할 수 있다.

하나의 RecyclerView 내부에 CardView가 들어가 있는 형태라고 보면 된다.


기본적인 설명은 https://www.journaldev.com/10024/android-recyclerview-android-cardview-example-tutorial 사이트를 참조하면 도움된다.


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.loopj.android:android-async-http:1.4.9' // 서버와의 통신 처리
    implementation 'com.squareup.picasso:picasso:2.5.2' // 이미지 라이브러리
    implementation 'com.android.support:recyclerview-v7:24.2.0' // ListView 개선 버전
    implementation "com.android.support:cardview-v7:24.2.0"
}


android:layout_margin="1dp" 로 했더니 깔끔하지 않아서 android:layout_marginBottom="1dp" 로 처리

cardCornerRadius : 레이아웃에 모서리 반지름 설정

cardBackgroundColor : 카드의 배경색을 설정

contentPadding : 자식 뷰 사이의 내부 간격을 조정

cardElevation : 그림자가 있는 카드를 생성


<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dp"
    app:cardCornerRadius="1dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="12dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <ImageView
            android:id="@+id/list_cell_image"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:src="@drawable/expose_btn_n" />

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_weight="4"
            android:orientation="vertical">

            <TextView
                android:id="@+id/list_cell_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="타이틀"
                android:textSize="20dp"
                android:textStyle="bold" />

        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>



API 28 이상에서는

<android.support.v7.widget.CardView 대신에 <androidx.cardview.widget.CardView 로 변경해야 한다.


앱 build.gradle 에서도

implementation 'androidx.cardview:cardview:1.0.0' 를 사용해야 한다.

블로그 이미지

Link2Me

,