Android Material设计中列表和卡片的创建方法解析

2019-12-10 18:29:23于海丽

创建卡片
CardView继承自FrameLayout,以卡片式显示一致的外观。它可以有阴影和圆角
创建一个有阴影的卡片,使用card_view:cardElevation属性。

使用这些属性来定制CardView组件的外观:

  • 在你的布局设置圆角半径,使用card_view:cardCornerRadius属性
  • 在代码中设置圆角半径,使用CardView.setRadius方法
  • 设置卡片的背景颜色,使用card_view:cardBackgroundColor属性

    <LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
      xmlns:tools="http://www.easck.com/tools" 
      xmlns:card_view="http://www.easck.com/apk/res-auto" 
      ... > 
      <!-- A CardView that contains a TextView --> 
      <android.support.v7.widget.CardView 
        xmlns:card_view="http://www.easck.com/apk/res-auto" 
        android:id="@+id/card_view" 
        android:layout_gravity="center" 
        android:layout_width="200dp" 
        android:layout_height="200dp" 
        card_view:cardCornerRadius="4dp"> 
     
        <TextView 
          android:id="@+id/info_text" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" /> 
      </android.support.v7.widget.CardView> 
    </LinearLayout> 
    
    

    添加依赖:
    gradle依赖

    dependencies { 
      ... 
      compile 'com.android.support:cardview-v7:21.0.+' 
      compile 'com.android.support:recyclerview-v7:21.0.+' 
    }