(4.0.34)CardView的基本用法

引用

  • Gradle 依赖

    compile 'com.android.support:cardview-v7:23.2.0'
  • 主布局引用(可用于RecyclerView的item布局)

    <android.support.v7.widget.CardView
          android:id="@+id/cardview"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="14dp"
          card_view:cardBackgroundColor="@color/colorAccent"
          card_view:cardCornerRadius="10dp"
          card_view:cardElevation="5dp"
          card_view:contentPadding="8dp">
          <!--子布局控件-->
    </android.support.v7.widget.CardView>
  • Cardview继承自FrameLayout,所以子控件布局规则和FrameLayout的一样

CardView的常用属性

 

属性作用
card_view:cardElevation阴影的大小
card_view:cardMaxElevation阴影最大高度
card_view:cardBackgroundColor卡片的背景色
card_view:cardCornerRadius卡片的圆角大小
card_view:contentPadding卡片内容于边距的间隔
card_view:contentPaddingBottom卡片内容与底部的边距
card_view:contentPaddingTop卡片内容与顶部的边距
card_view:contentPaddingLeft卡片内容与左边的边距
card_view:contentPaddingRight卡片内容与右边的边距
card_view:contentPaddingStart卡片内容于边距的间隔起始
card_view:contentPaddingEnd卡片内容于边距的间隔终止
card_view:cardUseCompatPadding设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式
card_view:cardPreventConrerOverlap在V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠

 

 

 

下面看效果: 
http://blog.youkuaiyun.com/javacainiao931121/article/details/51704672 
这是不加CardView是的效果的链接,如图: 
没有添加CardView的效果图片 
这是添加了CardView的效果图: 
这是添加了CardView的效果图

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" tools:ignore="MissingConstraints"> <LinearLayout android:id="@+id/ll_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:ignore="MissingConstraints"> <LinearLayout android:id="@+id/ll_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" tools:ignore="MissingConstraints"> <!-- 返回按钮 --> <ImageView android:id="@+id/btn_back" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginStart="16dp" android:padding="8dp" android:src="@drawable/img_back" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <!-- 分享按钮 --> <ImageView android:id="@+id/btn_share" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:padding="8dp" android:src="@drawable/img_share" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout> <LinearLayout android:id="@+id/ll_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 主图片 - 调整高度为0dp实现自适应 --> <ImageView android:id="@+id/image_main" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginEnd="16dp" android:scaleType="centerCrop" android:src="@drawable/xiaomi" app:layout_constraintBottom_toTopOf="@+id/details_container" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/phone_desc" app:layout_constraintTop_toBottomOf="@id/ll_label" /> <!-- 手机描述 - 移动到图片左侧 --> <LinearLayout android:id="@+id/phone_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:orientation="vertical" app:layout_constraintBottom_toTopOf="@+id/details_container" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ll_label"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名称:" /> <TextView android:id="@+id/phone_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小米14" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="价格:" /> <TextView android:id="@+id/phone_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥4399" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/spacs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="规格:" /> <TextView android:id="@+id/phone_spacs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="16G/256G" /> </LinearLayout> </LinearLayout> </LinearLayout> <!-- 详情内容容器 - 自适应屏幕宽度 --> <androidx.cardview.widget.CardView android:id="@+id/details_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:layout_marginBottom="24dp" app:cardCornerRadius="8dp" app:cardElevation="4dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/image_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小米14 详细介绍" android:textSize="18sp" android:textStyle="bold" android:paddingBottom="8dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="小米14搭载骁龙8 Gen3处理器,采用6.36英寸2K直屏,支持120Hz高刷新率,兼顾性能与续航。影像系统升级为5000万像素三摄,主摄使用索尼IMX900传感器,支持OIS光学防抖,拍照画质更上一层楼。" android:lineSpacingExtra="4sp" android:paddingBottom="12dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="产品特点" android:textSize="16sp" android:textStyle="bold" android:paddingTop="16dp" android:paddingBottom="4dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 性能:骁龙8 Gen3 + LPDDR5X + UFS 4.0,安兔兔跑分超200万" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 屏幕:6.36英寸华星光电C8直屏,2K分辨率,120Hz刷新率,2160Hz高频PWM调光" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 影像:5000万主摄(IMX900)+5000万超广角+5000万人像,OIS+EIS双防抖" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 续航:4610mAh电池+120W有线快充+50W无线快充,19分钟充满" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 系统:MIUI 15基于Android 14,支持光子引擎,流畅度提升30%" /> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout>这样设计为什么图片加载不出来
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值