Android Development--cardview Use

Source: Internet
Author: User

Android5.0 introduces us to a new control –cardview, which, in essence, can be seen as a framelayout that adds rounded corners and shadow effects on top of itself. Note: CardView is packaged as a layout and is often used as a container in the ListView and Recyclerview item layouts.

CardView should be used when displaying hierarchical content, and should be chosen when displaying lists or grids, as these edges can make it easier for users to differentiate between them.

Using CardView

First, assume that your layout looks like the following:

< Framelayout xmlns:app= "http://schemas.android.com/apk/ Res-auto "xmlns:android=" http://schemas.android.com/apk/ Res/android "android:layout_width=" match_parent "android:layout_height= "wrap_content" > <!--Main Content View--< Relativelayout> ... </relativelayout></FRAMELAYOUT>            

To create a card using the layout above, first you need to import the supported dependent libraries (Android-support-v7-cardview jar packages) in your Build.gradle file.

dependencies {    ...    compile ‘com.android.support:cardview-v7:21.0.2‘}

Now replace the framelayout with the CardView,

< Android.support.v7.widget.CardView xmlns:card_view= "http ://schemas.android.com/apk/res-auto "xmlns:android=" http ://schemas.android.com/apk/res/android "android:layout_width=  "match_parent" android:layout_height=  "wrap_content" > <!--Main content View--<relativelayout> ... </relativelayout></ANDROID.SUPPORT.V7.WIDGET.CARDVIEW>         

That's it! Using a dependent library will ensure that your program is stable and compatible with previous versions, although they are handled differently in Androidl and previous versions of Android.

Custom CardView

CardView provides a default elevation (meaning CardView z-axis shading) and fillet angle, so each card can maintain the same appearance on different devices. However, you can also customize these values to suit your needs.

<Android.support.v7.widget.CardViewxmlns:card_view= "Http://schemas.android.com/apk/res-auto" xmlns:android=< Span class= "Hljs-value" > "http://schemas.android.com/apk/res/android" android:layout _width= "match_parent" android:layout_height=" wrap_content "card_view:cardcornerradius=" 8DP "card_view:cardelevation=" 8DP "> <!--Main Content View--< Relativelayout> ... </relativelayout></ANDROID.SUPPORT.V7.WIDGET.CARDVIEW>         

Note: The Cardelevation property is used to determine the size and softness of the shadow so that it can realistically simulate a description of the depth effect.

Add Ripple Click Effect

By default, CardView is not clickable and does not have any touch feedback effects. Touch feedback animations can give users visual feedback when they click CardView. To implement this behavior, you must provide properties:

<android.support.v7.widget.CardView  ...  android:clickable="true"  android:foreground="?android:attr/selectableItemBackground">  ...</android.support.v7.widget.CardView>

Using the android:foreground= "? Android:attr/selectableitembackground" can make the CardView click to produce ripple effect, have the touch point outward spread.

Support for earlier versions

On the device prior to Androidl, CardView with the padding to support rounded corners, the rounded cut operation can be considered expensive. Similarly, for the shadow effect, before androidl, the padding is also provided to draw the shaded area, which padding is related to the elevation attribute, according to the document:

The padding value is:
The left and right values are: Maxcardelevation + (1-COS45) * Cornerradius
The values for the upper and lower sides are: Maxcardelevation * 1.5 + (1-COS45) * Cornerradius

Therefore, if you need to add padding to your content, you need to use the new attribute: card_view:contentpadding
Similarly, if you change the background of the CardView, you also need to use the new attribute: Card_view:cardbackgroundcolor

Android Development--cardview Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.