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

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

2016_02_19_CardView

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

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := tests

LOCAL_PACKAGE_NAME := CardView

LOCAL_SRC_FILES := \
    $(call all-java-files-under, src)

LOCAL_PROGUARD_ENABLED := disabled

#LOCAL_SDK_VERSION := 20

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

LOCAL_RESOURCE_DIR := \
    frameworks/support/v7/cardview/res \
    $(LOCAL_PATH)/res

LOCAL_AAPT_FLAGS := --auto-add-overlay --extra-packages android.support.v7.cardview

#LOCAL_JARJAR_RULES := \
#   $(LOCAL_PATH)/jarjar-rules.txt

include $(BUILD_PACKAGE)

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

$ cat << EOF > jarjar-rules.txt
rule android.support.v7.cardview.R* com.example.android.cardview.@0
EOF

不知道这个是与系统有关: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.

相关的参考文档:

  1. http://developer.android.com/reference/android/support/v7/widget/CardView.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注