woong's

Android CardView 사용하기 본문

Develop/Android

Android CardView 사용하기

dlsdnd345 2016. 2. 14. 18:27
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android CardView 사용하기


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

CardView 를 사용해서 기존에 ListView 에서 구현한던 것을 속성,  옵션으로 간단하게 적용 할수 있습니다.


cardView 는 기존의 리스트뷰에서 row 를 만들어 쓸때 라운드 처리 , row 테두리의 Dim처리등 이런 효과들을

편리하게 도와주는 view 입니다.



1. CardView 사용 준비 과정


cardView 를 사용하려면 라이브러리가 필요 합니다.

app build.gradle 에 아래 라이브러리를 추가 합니다.


1
2
3
4
5
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:recyclerview-v7:21.+'
    compile 'com.android.support:cardview-v7:21.+'
}
cs



2. cardView 옵션


위와 같이 적용하면 이제 cardView 를 사용 할수 있습니다. 

기본 큰 구조는 RecyclerView 로 구성 하였습니다.

구성후에 내부 row 를 cardView 로서 사용 하고 있습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="5dp"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">
 
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="10">
 
            <ImageView
                android:id="@+id/img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
 
        </RelativeLayout>
 
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="40">
 
            <TextView
                android:id="@+id/textTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:textStyle="bold" />
 
            <TextView
                android:id="@+id/textArtist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textTitle"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:textStyle="bold" />
 
        </RelativeLayout>
 
 
    </LinearLayout>
 
</android.support.v7.widget.CardView>
 
cs



Row 전체 코드 입니다. 내부의 코드는 화면 구성 코드 입니다. 핵심코드는 상단의 코드가 되겠습니다.


1
2
3
4
5
6
7
8
9
10
 
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="5dp"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
cs


핵심 코드 입니다. 핵심 코드가 CardView 로 구성되어 있고 속성들이 하나하나 중요 한것 같습니다.


cardCornerRadius : 카드 코너를 둥그렇게 조정 할 수 있는 속성 입니다.

cardElevation : 카드를 들어 올려서 카드 주변에 Dim 처리가 적용 됩니다.

cardBackgorundColor : 카드 배경 색을 지정 할 수 있습니다.



위와 같이 여러 가지 속성이 있습니다.



Comments