Android应用开发中CardView的初步使用指南

2019-12-10 19:02:54丽君

意思就是在做AndroidoManifest.xml编译时,采用替换的策略,即全部使用build.gradle中定义的属性作为最终属性。添加完成后,再次编译,问题解决。

使用

在使用CardVIew之前,要明白CardView是个什么东西。CardView如Linearlayout、Framelayout一样都是ViewGroup,即其他控件的容器。CardView继承于Framelayout,所以Framelayout的属性他都有,同时CardView还有几个特殊的属性:

在API21(Android L)等级以上拥有属性elevation,意为CardView的Z轴阴影,只有L平台有效。只能通过xml中的elevation属性指定;
其余(2.0以上)有属性cardBackgroundColor,意为CardView的卡片颜色,只能通过xml的cardBackgroundColor进行指定;
其余(2.0以上)有属性cardConerRadius,意为CardView卡片的四角圆角矩形程度,单位dimen(dp px sp),可以通过xml指定,也可以通过代码中的setRadius指定。

示例:

<android.support.v7.widget.CardView xmlns:android="http://www.easck.com/apk/res/android" 
 xmlns:app="http://www.easck.com/apk/res-auto" 
 android:id="@+id/cardview" 
 app:cardCornerRadius="8dp" 
 app:cardBackgroundColor="@color/black" 
 android:layout_margin="8dp" 
 android:layout_height="80dp" 
 android:layout_width="match_parent"> 
 
 <TextView 
  android:text="TextView in CardView" 
  android:layout_gravity="center" 
  android:textSize="26sp" 
  android:textColor="@color/l_white" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" /> 
</android.support.v7.widget.CardView> 

效果图:

Android应用开发中CardView的初步使用指南

CardView继承至FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影。CardView是一个Layout,可以布局其他View。