Android5.0增加了一些新的控件,不過說實話CardView還真沒咋用過呢,趁着休息學習一下CardView,翻譯過來就是卡片,看了一下這個View的特有屬性很少,它繼承自FrameLayout,能爲我們提供帶圓角的ViewGroup容器,免去了我們自己需要生成shape的煩惱。
使用步驟
首先需要添加V7包依賴:compile ‘com.android.support:cardview-v7:25.3.1’
在佈局中使用它:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_centerInParent="true"
app:cardBackgroundColor="#ffd300"
app:cardCornerRadius="30dp"
></android.support.v7.widget.CardView>
基本效果是這樣的:
在CardView裏面添加一些子View:
` <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ff0000"
android:gravity="center"
android:text="測試數據測試數據測試數據測試數據測試數據測試數據測試數據測試數據測試數據" />
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:background="#ff00ff"
android:gravity="center"
android:text="也是測試數據" />`
效果圖是這樣的:
當然了CardView還有幾種屬性可以設置,可以嘗試一下:
<!--app:cardUseCompatPadding="true"-->
<!-- app:cardMaxElevation="20dp"-->
<!--app:cardPreventCornerOverlap="false"-->
<!-- app:contentPaddingLeft/Right/Top/Bottom="20dp"-->
<!--app:cardElevation="10dp"
app:contentPadding="10dp"-->