A guide to the use of CardView in Android application development _android

Source: Internet
Author: User

Introduction

On Google I/O 2014, Google has released the Android L preview version, and this version of the UI has changed a lot, it's awesome! At the same time, Google also gave two can be backward-compatible controls into the V7 package, respectively, Recyclerview and CardView, this blog is about how to use CardView.

Where's CardView's bag?

Although CardView is integrated into the V7, it is not found in support-v7 that the name Cardview-v7 is found by looking at the files under the SDK extra path.

There are cardview-related things under this path, including the already packaged AAR package.

Depend on

If the application is built with Gradle, it is easy to refer to CardView:

dependencies { 
 Compile filetree (dir: ' Libs ', include: [' *.jar ']) 
 compile ' com.android.support:cardview-v7 : 21.0.0-rc1 ' 
} 

If Gradle is not available, prove that your SDK needs to be updated, and update the Google support package to the latest.

If you do not build with gradle, you need to find the jar bundle reference in which the jar package is hidden in the CardView AAR file:

Open the AAR as a package and extract the Class.jar, the jar file can be relied upon as a library file.

After relying on the completion of the addition, the compilation will find MINSDK error, cardview-v7 minsdk as "L", in fact, CardView can be backward to 2.0.
After the Android Gradle Tools 0.11 version, node replacement can be done through the tools configuration in XML, and Androdistudio support for Gradle tools is better than Parentide IntelliJ. So at present I can find the normal use of CardView IDE and configuration method only in Androidstudio, but this is also the trend, early use good things.

Solution:

In the androidmanifest.xml of the project using CardView:

<USES-SDK 
  xmlns:tools= "Http://schemas.android.com/tools" 
  tools:node= "Replace"/> 

This means that when you do Androidomanifest.xml compilation, you take a replacement policy, that is, to use all the attributes defined in Build.gradle as the final attribute. After the addition is complete, compile again and the problem is resolved.

Use

Before you use CardView, understand what CardView is. CardView, like LinearLayout and Framelayout, are viewgroup, which are containers for other controls. CardView inherits from Framelayout, so framelayout attributes are available to him, and CardView has several special attributes:

The API21 (Android L) level has attribute elevation, meaning CardView's z-axis shadow, only the L platform is valid. Can only be specified through the elevation attribute in XML;
The rest (more than 2.0) has attribute Cardbackgroundcolor, meaning CardView card color, can only be specified through the cardbackgroundcolor of XML;
The remainder (more than 2.0) has attribute Cardconerradius, meaning the rounded corners of the CardView card, the unit dimen (DP px SP), can be specified by XML, or by Setradius in code.

Example:

<android.support.v7.widget.cardview xmlns:android= "Http://schemas.android.com/apk/res/android" 
 Xmlns:app = "Http://schemas.android.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> 

Effect Chart:

CardView inherits to the Framelayout class, which can be displayed in a consistent display in a card layout that can contain rounded corners and shadows. CardView is a layout that can be laid out in other view.

CardView Common Properties:

    • Card_view:cardelevation the size of the shadow
    • Card_view:cardmaxelevation Maximum height of shadow
    • Background color of Card_view:cardbackgroundcolor card
    • The fillet size of the Card_view:cardcornerradius card
    • Card_view:contentpadding card content at intervals of margins
    • Card_view:contentpaddingbottom card content and bottom margin
    • Card_view:contentpaddingtop card content and top margin
    • Card_view:contentpaddingleft card contents and left margin
    • Card_view:contentpaddingright Card contents to the right margin
    • Card_view:contentpaddingstart card content at interval start of margin
    • Card_view:contentpaddingend card content at interval end of margin
    • Card_view:cardusecompatpadding sets the inner margin, the v21+ version and the previous version still have the same calculation method
    • Card_view:cardpreventconreroverlap adds an inner margin to the V20 and previous versions to prevent overlapping of content and corners

Let's look at one more example:

<android.support.v7.widget.cardview xmlns:card_view= "Http://schemas.android.com/apk/res-auto"
  android: Layout_width= "200DP"
  android:layout_height= "wrap_content"
  card_view:cardbackgroundcolor= "#303069"
  card_view:cardcornerradius= "10DP"
  card_view:cardpreventcorneroverlap= "true"
  Card_view: Cardusecompatpadding= "true"
  card_view:contentpadding= "10DP" > ...
</android.support.v7.widget.CardView>

Note: Add ' compile ' com.android.support:cardview-v7:21.+ ' to the dependencies of Gradle
The cards in the view will then basically display this way:

CardView inherits to the Framelayout class, which can be displayed in a consistent display in a card layout that can contain rounded corners and shadows. CardView is a layout that can be laid out in other view.

Related Article

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.