一. 什么是CardView?
1.1官方介紹
CardView擴(kuò)展 FrameLayout類并讓您能夠顯示卡片內(nèi)的信息,這些信息在整個平臺中擁有一致的呈現(xiàn)方式。CardView小部件可擁有陰影和圓角。
1.2效果圖

CardView里面嵌套TextView
二.用法
2.1使用步驟
- 在build.gradle中加入
dependencies {
compile 'com.android.support:cardview-v7:version'
}(version為版本號)
- xml中使用布局
- 注意:因為CardView繼承自FrameLayout,因此其子控件一般只有一個或者是<ViewGroup>
<android.support.v7.widget.CardView
android:id="@+id/hello_world_card"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="3dp"
app:cardElevation="@dimen/cardview_default_elevation"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:gravity="center"
android:textSize="24sp"/>
</android.support.v7.widget.CardView>
- 添加點(diǎn)擊事件
mCardView = (CardView) findViewById(R.id.hello_world_card);
mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你點(diǎn)擊了CardView",Toast.LENGTH_SHORT).show();
}
});
三.xml屬性詳細(xì)介紹 官方鏈接
- 注意:其中android.support.v7.cardview在xml中就是app(自定義布局的前綴)
android.support.v7.cardview:cardBackgroundColor
CardView的背景顏色.值為rgb或者argb顏色值(如#ffffff)
android.support.v7.cardview:cardCornerRadius
CardView的圓角弧度.值為dimension值(如4dp)
android.support.v7.cardview:cardElevation
CardView的陰影高度。值為dimension值(如4dp)
android.support.v7.cardview:cardMaxElevation
CardView的最大陰影高度。值為dimension值(如4dp),自己沒看出來有什么效果
android.support.v7.cardview:contentPadding
Cardview內(nèi)子控件與CardView邊緣的邊距。值為dimension值(如4dp)
android.support.v7.cardview:cardUseCompatPadding
在xml文件中設(shè)置內(nèi)邊距,V21+的版本和之前的版本仍舊具有一樣的計算方式
android.support.v7.cardview:cardPreventConrerOverlap
在xml文件中設(shè)置內(nèi)邊距,在V20和之前的版本中添加內(nèi)邊距,這個屬性為了防止內(nèi)容和邊角的重疊
四.源碼
- MainActivity
public class MainActivity extends AppCompatActivity {
private CardView mCardView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCardView = (CardView) findViewById(R.id.hello_world_card);
mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你點(diǎn)擊了CardView",Toast.LENGTH_SHORT).show();
}
});
}
}
- XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.foxnickel.recyclerview.MainActivity"
android:padding="8dp">
<android.support.v7.widget.CardView
android:id="@+id/hello_world_card"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="3dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:gravity="center"
android:textSize="24sp"/>
</android.support.v7.widget.CardView>
</RelativeLayout>