CardView
CardView
效果图
依赖
dependencies {
……
compile 'com.android.support:cardview-v7:23.0.+'
}
Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:kongqw="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.kongqw.cardviewdemo.MainActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="20dp"
kongqw:cardBackgroundColor="#FF00AABB"
kongqw:cardCornerRadius="30dp"
kongqw:cardElevation="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CardView"
android:textColor="#FF000000"
android:textSize="30dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="20dp"
kongqw:cardCornerRadius="10dp"
kongqw:cardElevation="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bb" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CardView"
android:textColor="#FFFFFFFF"
android:textSize="30dp" />
</android.support.v7.widget.CardView>
</LinearLayout>
总结
XML属性
Attribute Name | Related Method | Description |
---|---|---|
android.support.v7.cardview:cardBackgroundColor | setCardBackgroundColor(int) | Background color for CardView. |
android.support.v7.cardview:cardCornerRadius | setRadius(float) | Corner radius for CardView. |
android.support.v7.cardview:cardElevation | setMaxCardElevation(float) | Elevation for CardView. |
android.support.v7.cardview:cardMaxElevation | Maximum Elevation for CardView. | |
android.support.v7.cardview:cardPreventCornerOverlap | setPreventCornerOverlap(boolean) | Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners. |
android.support.v7.cardview:cardUseCompatPadding | setUseCompatPadding(boolean) | Add padding in API v21+ as well to have the same measurements with previous versions. |
android.support.v7.cardview:contentPadding | setContentPadding(int,int,int,int) | Inner padding between the edges of the Card and children of the CardView. |
android.support.v7.cardview:contentPaddingBottom | setContentPadding(int,int,int,int) | Inner padding between the bottom edge of the Card and children of the CardView. |
android.support.v7.cardview:contentPaddingLeft | setContentPadding(int,int,int,int) | Inner padding between the left edge of the Card and children of the CardView. |
android.support.v7.cardview:contentPaddingRight | setContentPadding(int,int,int,int) | Inner padding between the right edge of the Card and children of the CardView. |
android.support.v7.cardview:contentPaddingTop | setContentPadding(int,int,int,int) | Inner padding between the top edge of the Card and children of the CardView. |
方法
返回值类型 | 方法/描述 |
---|---|
float | getCardElevation() Returns the backward compatible elevation of the CardView. |
int | getContentPaddingBottom() Returns the inner padding before the Card’s bottom edge |
int | getContentPaddingLeft() Returns the inner padding after the Card’s left edge |
int | getContentPaddingRight() Returns the inner padding before the Card’s right edge |
int | getContentPaddingTop() Returns the inner padding after the Card’s top edge |
float | getMaxCardElevation() Returns the backward compatible elevation of the CardView. |
boolean | getPreventCornerOverlap() Returns whether CardView should add extra padding to content to avoid overlaps with rounded corners on API versions 20 and below. |
float | getRadius() Returns the corner radius of the CardView. |
boolean | getUseCompatPadding() Returns whether CardView will add inner padding on platforms L and after. |
void | setCardBackgroundColor(int color) Updates the background color of the CardView |
void | setCardElevation(float radius) Updates the backward compatible elevation of the CardView. |
void | setContentPadding(int left, int top, int right, int bottom) Sets the padding between the Card’s edges and the children of CardView. |
void | setMaxCardElevation(float radius) Updates the backward compatible elevation of the CardView. |
void | setPadding(int left, int top, int right, int bottom) Sets the padding. |
void | setPaddingRelative(int start, int top, int end, int bottom) Sets the relative padding. |
void | setPreventCornerOverlap(boolean preventCornerOverlap) On API 20 and before, CardView does not clip the bounds of the Card for the rounded corners. |
void | setRadius(float radius) Updates the corner radius of the CardView. |
void | setUseCompatPadding(boolean useCompatPadding) CardView adds additional padding to draw shadows on platforms before L. |