cardview默認情況下會有陰影,如果我們不需要陰影效果,需要進行設置。
我們先來看下cardview常見的屬性
屬性 | 說明 |
---|---|
app:cardElevation | 陰影的大小 |
app:cardMaxElevation | 陰影最大高度 |
app:cardBackgroundColor | 卡片的背景色 |
app:cardCornerRadius | 卡片的圓角大小 |
app:contentPadding | 卡片內容於邊距的間隔 |
app:contentPaddingBottom | 卡片內容與底部的邊距 |
app:contentPaddingTop | 卡片內容與頂部的邊距 |
app:contentPaddingLeft | 卡片內容與左邊的邊距 |
app:contentPaddingRight | 卡片內容與右邊的邊距 |
app:contentPaddingStart | 卡片內容於邊距的間隔起始 |
app:contentPaddingEnd | 卡片內容於邊距的間隔終止 |
app:cardUseCompatPadding | 在Android 5.0及以上平臺中,設置是否要添加padding,5.0以下默認添加padding。默認值爲false |
app:cardPreventCornerOverlap | 是否給content添加padding,來阻止與圓角重疊,默認值爲true |
由此可知,我們只需要把app:cardElevation
和app:cardMaxElevation
設爲0dp
,即可達到沒有陰影的cardview的效果。
xml 代碼如下
<androidx.cardview.widget.CardView
app:cardBackgroundColor="@color/white"
android:layout_width="300dp"
android:layout_height="300dp"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:cardUseCompatPadding="true">
<ImageView
android:id="@+id/image_ad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</androidx.cardview.widget.CardView>