在有些項目中要求圖片有陰影郭脂,并且邊角為橢圓可帽,這時就要考慮是否使用CardView(卡片布局)。
我們首先需要導(dǎo)入依賴包
implementation 'com.android.support:cardview-v7:28.0.0'
然后嘲驾,了解一下CardView基本寫法
<android.support.v7.widget.CardView
android:id="@+id/cv_demo"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="50dp"
android:layout_centerInParent="true">
</android.support.v7.widget.CardView>
接下來,開始講解一下CardView的基本屬性:
- app:cardBackgroundColor="@color/colorPrimary":添加背景
圖片.png
- app:cardElevation="18dp":設(shè)置陰影大小
圖片.png
app:cardMaxElevation="18dp":設(shè)置最大陰影大小
app:cardPreventCornerOverlap="true":在v20和之前的版本中添加內(nèi)邊距迹卢,這個屬性是為了防止卡片內(nèi)容和邊角的重疊辽故,考慮到兼容性,還是將這個屬性置為true比較好
app:cardUseCompatPadding="true":在在API21之前和之后的版本中腐碱,陰影部分的大小有差異性誊垢,為了兼容性考慮,將cardUseCompatPadding屬性置為true比較好
app:cardCornerRadius="10dp":為四個角設(shè)置圓角
圖片.png
-
contentPadding喻杈、contentPaddingLeft彤枢、contentPaddingRight、contentPaddingBottom筒饰、contentPaddingBottom:設(shè)置內(nèi)邊距
app:contentPadding="10dp" app:contentPaddingLeft="10dp" app:contentPaddingRight="10dp" app:contentPaddingBottom="10dp" app:contentPaddingTop="10dp"
到這里缴啡,CardView的屬性已經(jīng)講完了。
CardView其實是一個ViewGroup瓷们,它可以添加子布局业栅,假如添加一張圖片:
[設(shè)置了內(nèi)邊距]
圖片.png
[沒有設(shè)置內(nèi)邊距]
圖片.png
xml代碼如下:
<android.support.v7.widget.CardView
android:id="@+id/cv_demo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_centerInParent="true"
app:cardBackgroundColor="@color/colorPrimary"
app:cardElevation="18dp"
app:cardMaxElevation="18dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:cardCornerRadius="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@mipmap/pic2"/>
</android.support.v7.widget.CardView>
[本章完...]