前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学会使用CardView,简单实现卡片式布局效果

学会使用CardView,简单实现卡片式布局效果

作者头像
分享达人秀
发布2018-02-05 14:31:19
2.7K0
发布2018-02-05 14:31:19
举报
文章被收录于专栏:分享达人秀分享达人秀

还记得我们一共学过了多少UI控件了吗?都掌握的怎么样啊

安卓中一些常用控件学习得差不多了,今天再来学习一个新的控件CardView,在实际开发中也有非常高的地位。

一、CardView简介

CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果。

CardView继承自Framelayout,所以FrameLayout所有属性CardView均可以直接拿来用,不过CardView还有自己独有的属性,常用属性如下:

  • app:cardElevation:设置阴影的大小。
  • app:cardMaxElevation:设置阴影最大高度。
  • app:cardBackgroundColor:设置卡片的背景色。
  • app:cardCornerRadius:设置卡片的圆角大小。
  • app:contentPadding:设置内容的padding。
  • app:contentPaddingTop:设置内容的上padding。
  • app:contentPaddingLeft:设置内容的左padding。
  • app:contentPaddingRight:设置内容的右padding。
  • app:contentPaddingBottom:设置内容的底padding。
  • app:cardUseCompatPadding:是否使用CompatPadding。
  • app:cardPreventConrerOverlap:是否使用PreventCornerOverlap。

这里有一点需要值得注意,之前学习到的控件属性都是android:开头的,而这里所列的属性是app:开头的,如果继续使用默认的会提示找不见对应属性,需要我们定义一个app命名空间,在布局文件中需要加入xmlns:app="http://schemas.android.com/apk/res-auto"语句,具体见后续案例,这里不作过多介绍,后续再详细学习。

二、CardView示例1

接下来通过几个简单的小示例程序来进一步学习CardView。

继续使用WidgetSample工程的advancedviewsample模块,首先需要添加支持库,具体操作步骤同之前分享的揭开RecyclerView庐山真面目,这里不再重复分享。这次输入的关键字是cardview,即可完成CardView依赖库的添加。

在src/main/res/layout/目录下创建cardview_layout.xml文件,在其中填充如下代码片段:

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

    <android.support.v7.widget.CardView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp">        <TextView            android:layout_width="match_parent"            android:layout_height="70dp"            android:text="正常使用效果"            android:gravity="center_horizontal|center_vertical"            android:textSize="20sp"            android:padding="10dp"            android:layout_margin="10dp"/>    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        app:cardBackgroundColor="#669900"        app:cardCornerRadius="10dp">    <TextView            android:layout_width="match_parent"            android:layout_height="70dp"            android:text="设置背景和标签"            android:gravity="center_horizontal|center_vertical"            android:textSize="20sp"            android:padding="20dp"            android:layout_margin="10dp"/>    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        app:cardBackgroundColor="#874528"        app:cardElevation="20dp"        app:cardCornerRadius="10dp">        <TextView            android:layout_width="match_parent"            android:layout_height="70dp"            android:text="设置阴影"            android:gravity="center_horizontal|center_vertical"            android:textSize="20sp"            android:padding="10dp"            android:layout_margin="10dp"/>    </android.support.v7.widget.CardView></LinearLayout>

然后新建CardViewActivity.java文件,加载上面的布局文件,填充的代码如下:

代码语言:javascript
复制
public class CardViewActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.cardview_layout);    }}

修改启动的Activity,运行程序可以看到下图所示效果。

三、CardView示例2

CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为一种容器使用。CardView应该被使用在显示层次性的内容时;在显示列表或网格时更应该被选择,因为这些边缘可以使得用户更容易去区分这些内容。

接下来简单定义一个CardView的item项,并在Java代码中修改CardView的属性,关于结合ListView和RecyclerView的部分比较简单,这里不做过多介绍。

继续再上一个案例的基础上进行修改,修改后的cardview_layout.xml文件代码如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/cardview"    android:layout_width="match_parent"    android:layout_height="200dp"    android:layout_margin="15dp">    
<ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true"        android:scaleType="centerCrop"        android:src="@drawable/image_01"/>    
<TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginBottom="10dp"        android:layout_marginRight="10dp"        android:clickable="true"        android:gravity="right|bottom"        android:text="CardView作为item使用"        android:textColor="@android:color/white"        android:textSize="24sp"/>
</android.support.v7.widget.CardView>

继续修改CardViewActivity.java文件,获得CardView组件并动态修改其属性,修改后的代码如下:

代码语言:javascript
复制
package com.jinyu.cqkxzsxy.android.advancedviewsample;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;

/** * @创建者 鑫鱻 * @描述 Android零基础入门到精通系列教程,欢迎关注微信公众号ShareExpert */
public class CardViewActivity extends AppCompatActivity {    
private CardView mCardView = null;

    @Override    protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);                
    setContentView(R.layout.cardview_layout2);        
    mCardView = (CardView) findViewById(R.id.cardview);// 设置卡片圆角的半径大小      
          mCardView.setRadius(20);        // 设置卡片背景的颜色        
          mCardView.setCardBackgroundColor(Color.RED);        // 设置阴影部分大小        
          mCardView.setCardElevation(10);        // 设置卡片距离阴影大小        
          mCardView.setContentPadding(10, 10, 10, 10);    }}

重新运行程序,可以得到下图所示效果。

至此,CardView的学习到此告一段落,是不是发现使用起来也非常简单,更多用法建议自己去摸索。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档