Android控件<第十八篇>:CardView-卡片布局

在有些項目中要求圖片有陰影,并且邊角為橢圓,這時就要考慮是否使用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>

[本章完...]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容