CardView基本使用

CardView继承自FrameLayout,并且可以设置圆角和阴影,让卡片变得更具有立体感。

  1. 引入CardView

    implementation 'com.android.support:cardview-v7:27.1.1'
    

2)xml >> layout_cardview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_margin="20dp"
    app:cardCornerRadius="20dp"
    app:cardElevation="20dp"
    app:cardBackgroundColor="@color/cardview_dark_background"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        android:layout_gravity="center"
        android:scaleType="centerInside"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="@android:color/white"
        android:text="银行卡"/>

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

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="改变圆角和阴影的强度"/>

</LinearLayout>

因为继承自FrameLayout,可以看到CardView可以包容组件,这里面有两个属性实现了立体感的效果, app:cardCornerRadius圆角和 app:cardElevation阴影,app:cardBackgroundColor是设置卡片的背景,也可以在JAVA中设置

     btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cardView.setRadius(100);
            cardView.setCardElevation(100);
        }
    });

效果如下


CardView.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Material Design中有一种很个性的设计概念:卡片式设计(Cards),Cards拥有自己独特的UI特征...
    小庄bb阅读 3,035评论 1 4
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,019评论 3 119
  • CardView简介 CardView继承自FrameLayout类。 CardView是一种卡片视图,主要是以卡...
    上善若水Ryder阅读 34,024评论 9 52
  • Material Design中有一种很个性的设计概念:卡片式设计(Cards),Cards拥有自己独特的UI特征...
    滴滴滴9527阅读 45,457评论 3 64
  • 丽江的美,得用心去体会。不求艳遇,只求旅行。 大理去过很多趟了,这次只身一人来到丽江,发现丽江古镇要比大...
    刘显静阅读 597评论 2 4