Android: 在android-5.1.1_r15项目中编译CardView

CardView应用演示的是如何使用CardView widget (android-support-v7-cardview), 你可以设置转角的半径与阴影的大小,使卡片显示的效果达到最佳:


相关的代码在android-5.1.1_r15/development/samples/browseable/CardView下面,没有提供相应的编译脚本, 如果你想在项目中编译,相关的Android.mk可以这么写:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)



    $(call all-java-files-under, src)



# Card view support
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v7-cardview

    frameworks/support/v7/cardview/res \

LOCAL_AAPT_FLAGS := --auto-add-overlay --extra-packages

#   $(LOCAL_PATH)/jarjar-rules.txt

include $(BUILD_PACKAGE)

在某些情况下,将编译好的CardView安装到手机中,不能够正常运行,会出现runtime exception, 报找不到$。这时候你可以试试将*重命名为, 编写jarjar-rules文件:

$ cat << EOF > jarjar-rules.txt

不知道这个是与系统有关:ubuntu-14.04 x86_64下运行时就有问题,但在mac os 10.9下编译就没有这个问题;

还是跟JDK版本有关,mac os 10.9下使用的JDK版本为1.7.0_75。

Class Overview
A FrameLayout with a rounded corner background and shadow.

CardView uses elevation property on L for shadows and falls back to a custom shadow implementation on older platforms.

Due to expensive nature of rounded corner clipping, on platforms before L, CardView does not clip its children that intersect with rounded corners. Instead, it adds padding to avoid such intersection (See setPreventCornerOverlap(boolean) to change this behavior).

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 – cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 – cos45) * cornerRadius on top and bottom.

Since padding is used to offset content for shadows, you cannot set padding on CardView. Instead, you can use content padding attributes in XML or setContentPadding(int, int, int, int) in code to set the padding between the edges of the Card and children of CardView.

Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.

To change CardView’s elevation in a backward compatible way, use setCardElevation(float). CardView will use elevation API on L and before L, it will change the shadow size. To avoid moving the View while shadow size is changing, shadow size is clamped by getMaxCardElevation(). If you want to change elevation dynamically, you should call setMaxCardElevation(float) when CardView is initialized.