Android CardView

CardView 為繼承 FrameLayout 的 Class.

向下相容需要 support v7 程式庫 : In build.gradle
compile ‘com.android.support:appcompat-v7:23.3.0’

CardView 屬性介紹

cardBackgroundColor : 背景顏色

White

Black

cardCornerRadius : 圓角

cardview:cardCornerRadius = “15dp”

cardview:cardCornerRadius = “30dp”

cardview:cardElevation : Z軸高度

cardview:cardElevation = “0dp”

cardview:cardElevation = “10dp”

點擊效果

當CardView設置點擊時

可在xml 中設置 android:foreground=”?attr/selectableItemBackground”

Source Code

layout.xml
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
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="#ffffff"
card_view:cardCornerRadius="30dip"
card_view:contentPadding="10dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="10dp"
android:foreground="?attr/selectableItemBackground">
<ImageView
android:id="@+id/cardview_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/dsc_2013011902"
/>
<TextView
android:id="@+id/cardview_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_horizontal"
android:text="@string/photo_title"
android:textColor="#ffffff"
android:background="#000000"
/>
</android.support.v7.widget.CardView>
作者

Nick Lin

發表於

2016-05-25

更新於

2023-01-18

許可協議


評論