android CardView常用属性

本文详细介绍如何在Android开发中将CardView与RecyclerView结合使用,包括CardView的属性设置及在XML布局中的应用实例,帮助开发者掌握美观且实用的卡片式布局设计。

                        

 

使用环境:

            recyclerView + CardView 确实配,但不配合使用也没什么,所以别说绝配什么的。

使用方法:

          1 属性:

<resources>
    <declare-styleable name="CardView">
        <!-- Background color for CardView. -->
        <!-- 设置背景色 -->
        <attr name="cardBackgroundColor" format="color" />
        <!-- Corner radius for CardView. -->
        <!-- 设置圆角大小 -->
        <attr name="cardCornerRadius" format="dimension" />
        <!-- Elevation for CardView. -->
        <!-- 设置Z轴阴影高度 -->
        <attr name="cardElevation" format="dimension" />
        <!-- Maximum Elevation for CardView. -->
        <!-- 设置Z轴阴影最大高度 -->
        <attr name="cardMaxElevation" format="dimension" />
        <!-- Add padding in API v21+ as well to have the same measurements with previous versions. -->
        <!-- 设置内边距,v21+的版本和之前的版本仍旧具有一样的计算方式 -->
        <attr name="cardUseCompatPadding" format="boolean" />
        <!-- Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners. -->
        <!-- 在v20和之前的版本中添加内边距,这个属性是为了防止卡片内容和边角的重叠 -->
        <attr name="cardPreventCornerOverlap" format="boolean" />
        <!-- 下面是卡片边界距离内部的距离-->
        <!-- Inner padding between the edges of the Card and children of the CardView. -->
        <attr name="contentPadding" format="dimension" />
        <!-- Inner padding between the left edge of the Card and children of the CardView. -->
        <attr name="contentPaddingLeft" format="dimension" />
        <!-- Inner padding between the right edge of the Card and children of the CardView. -->
        <attr name="contentPaddingRight" format="dimension" />
        <!-- Inner padding between the top edge of the Card and children of the CardView. -->
        <attr name="contentPaddingTop" format="dimension" />
        <!-- Inner padding between the bottom edge of the Card and children of the CardView. -->
        <attr name="contentPaddingBottom" format="dimension" />
    </declare-styleable>
</resources>

         2 使用方法:

//列表配置
    implementation 'com.android.support:cardview-v7:26.1.0'

           xml布局中:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="adapter"
            type="com.tongyonghangkong.ui.adapter.DayAndDayTextAdapter" />

        <variable
            name="bean"
            type="String" />
    </data>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="@dimen/onehundredSixty"
        android:foreground="?attr/selectableItemBackground"
        android:gravity="center"
        android:layout_margin="@dimen/fifteen"
        android:orientation="vertical"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="15dp"
        app:cardMaxElevation="3dp"
        app:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/ivModule"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/icon_girl" />

            <TextView
                android:id="@+id/tvModuleFuncationName"
                style="@style/module_style_text_tv_1_12"
                android:text="@string/module_str_love_counseling" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</layout>

      效果:                    

                                              

   注意事项: cardView下只能展示一个布局,所以需要使用常用的LinerLayout / RelativeLayout等等……

 

 

 

 

 

 

 

<think>好的,我需要帮助用户了解Android CardView的详细用法和特性。首先,根据用户提供的引用资料,我需要整理CardView的基本介绍、主要属性、使用方法、常见问题和优化建议。 首先,CardView是Material Design的重要组件,用于创建卡片式布局。要使用它,需要在build.gradle中添加依赖项,不过新项目可能已经默认包含。然后,在布局文件中使用<androidx.cardview.widget.CardView>标签,并设置相关属性,如圆角半径、阴影高度等。用户提供的引用[4]和[5]提到,CardView在不同系统版本上表现不同,特别是阴影的实现方式,这点需要强调。 接下来是属性部分。根据引用[4]和[5],CardView常用属性包括cardCornerRadius、cardElevation、cardBackgroundColor等。这些属性可以通过XML或代码设置。需要详细说明每个属性的作用和取值范围,例如cardCornerRadius控制圆角大小,cardElevation影响阴影效果,而contentPadding可以调整内容与卡片边界的间距。 在应用案例部分,结合引用[1]和[2],可以给出一个简单的示例代码,展示如何在Activity或Fragment中引用CardView,并设置其属性。此外,提到与RecyclerView结合使用的情况,如引用[2]中的示例教程,说明CardView常用于列表项的布局设计。 关于常见问题,用户提到的引用[3]指出在低版本设备上CardView可能出现白边问题。需要分析原因,并给出解决方案,例如设置cardPreventCornerOverlap为false,或者调整内容边距。此外,引用[4]提到不同系统版本下CardView的尺寸差异,可能需要开发者注意适配问题。 最后,总结最佳实践,如合理使用圆角半径、避免过度阴影等,确保UI的兼容性和美观性。在生成相关问题部分,可以围绕性能优化、高级功能、适配问题和设计规范展开,帮助用户进一步学习。 现在需要检查是否有遗漏的信息,确保所有引用资料中的关键点都被涵盖,同时保持回答结构清晰,符合用户的要求。另外,要确保所有数学公式和代码块格式正确,使用中文回答,并正确添加引用标识。</think>### Android CardView 详细解析 #### 一、基础介绍 CardView 是 Material Design 的核心组件之一,用于实现卡片式布局效果,支持圆角、阴影和高度调节功能。在 Android 5.0+ 系统中使用原生阴影效果,低版本则通过兼容库模拟[^4]。 **依赖配置** 新项目默认包含 CardView 库,旧项目需在 `build.gradle` 中添加: ```groovy implementation 'androidx.cardview:cardview:1.0.0' ``` #### 二、核心属性详解 1. **外观控制** - `app:cardCornerRadius`:圆角半径(单位:dp),例如:`app:cardCornerRadius="8dp"`[^5] - `app:cardElevation`:阴影高度,影响立体感(5.0+ 原生实现,低版本模拟)[^4] - `app:cardBackgroundColor`:背景色,支持透明度调节 2. **边界与间距** - `app:cardUseCompatPadding`:设为 `true` 可兼容低版本的内边距差异 - `app:contentPadding`:内容与卡片边缘间距,避免内容紧贴圆角[^3] 3. **高级配置** - `app:cardPreventCornerOverlap`:设为 `false` 可消除低版本的白边问题 - `app:cardMaxElevation`:最大阴影高度限制 #### 三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值