Android中的CardView使用
Android 5.0 版本中新增了CardView,CardView继承自FrameLayout类,并且可以设置圆角和阴影,使得控件具有立体性,也可以包含其他的布局容器和控件。
1.配置build.gradle
如果SDK低于5.0,我们仍旧要引入v7包。在build.gradle 中加入如下代码已自动导入 support-v7包。记得配置完再重新Build一下工程。
compile 'com.android.support:appcompat-v7:22.2.1‘
compile 'com.android.support:cardview-v7:22.1.0'
2.使用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:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/cv_cardview"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
android:layout_centerInParent="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/aa"
android:scaleType="centerInside"/>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"> <SeekBar
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/sb_1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="控制圆角大小"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"> <SeekBar
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/sb_2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="控制阴影大小"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"> <SeekBar
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/sb_3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="控制图片间距大小"/>
</LinearLayout> </LinearLayout>
重要属性:
app:cardCornerRadius 设置圆角的半径
app:cardElevation 设置阴影的半径
其它属性:
app:cardBackgroundColor=""设置背景色
app:cardMaxElevation="" 设置Z轴最大高度值
app:cardUseCompatPadding="" 是否使用CompatPadding
app:cardPreventCornerOverlap="" 是否使用PreventCornerOverlap
app:contentPadding="" 内容的Padding
app:contentPaddingTop="" 内容的上Padding
app:contentPaddingLeft="" 内容的左Padding
app:contentPaddingRight="" 内容的右Padding
app:contentPaddingBottom="" 内容的下Padding
java代码:
package com.example.cardviewdemo; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.widget.SeekBar; public class MainActivity extends AppCompatActivity { /* app:cardBackgroundColor=""设置背景色
app:cardMaxElevation="" 设置Z轴最大高度值
app:cardUseCompatPadding="" 是否使用CompatPadding
app:cardPreventCornerOverlap="" 是否使用PreventCornerOverlap
app:contentPadding="" 内容的Padding
app:contentPaddingTop="" 内容的上Padding
app:contentPaddingLeft="" 内容的左Padding
app:contentPaddingRight="" 内容的右Padding
app:contentPaddingBottom="" 内容的下Padding*/ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CardView mCardview = (CardView) findViewById(R.id.cv_cardview);
SeekBar sb_1 = (SeekBar) findViewById(R.id.sb_1);
SeekBar sb_2 = (SeekBar) findViewById(R.id.sb_2);
SeekBar sb_3 = (SeekBar) findViewById(R.id.sb_3); sb_1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mCardview.setRadius(i);//设置圆角半径
} @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) { }
}); sb_2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mCardview.setCardElevation(i);//设置阴影半径
} @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) { }
}); sb_3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mCardview.setContentPadding(i, i, i, i);//设置cardView中子控件和父控件的距离
} @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
}
}
完成
Android中的CardView使用的更多相关文章
- 彻底理解 Android 中的阴影
如果我们想创造更好的 Android App,我相信我们需要遵循 Material Design 的设计规范.一般而言,Material Design 是一个包含光线,材质和投影的三维环境.如果我们想 ...
- ANDROID L——RecyclerView,CardView进口和使用(Demo)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控 ...
- Android 中的AlertDialog使用自定义布局
Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...
- Android中的LinearLayout布局
LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了, 线性布局是按照垂直方向(vertical)或水平方向 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
随机推荐
- DataType--时间类型
SQL SERVER 存储时间的方式和存放浮点数的方式类似,存放时按照一定公式算出一个数值,存放到页面,在读取时按照公式求算出时间值,再按照默认或指定的时间格式展示给用户. 如果存放DATETIME数 ...
- 百度云BDCloudVideoView播放器的初体验
今天试用了一下百度云BDCloudVideoView,记录下遇到的坑. 前面一切还好,按照他的要求各种导入,然后开始码代码,起实就是抄例子.然后各种坑开始了 下面这个你看名称能知道它是个啥吗? mVi ...
- thedao
TheDao 简化版解释 the Dao 合约 contract f1{ function transfer() { if (acccount[m]>=100) { m.send(100) ac ...
- 为什么 kubernetes 天然适合微服务 (1)
此文已由作者刘超授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 最近总在思考,为什么在支撑容器平台和微服务的竞争中,Kubernetes 会取得最终的胜出,事实上从很多角度出发 ...
- 861. Score After Flipping Matrix
We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...
- f(!gotop.length) return false;
if (!$("p").length) return; if语句接收一个布尔值,如果布尔值为true则执行接下来的语句,布尔值为false则执行else关键词后的语句. JavaS ...
- node创建一个简单的web服务
本文将如何用node创建一个简单的web服务,过程也很简单呢~ 开始之前要先安装node.js 1.创建一个最简单的服务 // server.js const http = require('http ...
- 使用过多的递归出现错误,“System.StackOverflowException”类型的未经处理的异常在 mscorlib.dll 中发生
class Program { static void Main(string[] args) { sub(0); } private static void sub(int count) { ...
- ubuntu下安装和更新R语言
R官网更新说明 https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu/README.html 本文主要讲解在ubuntu下如何安装和更新 ...
- C++_基础4-分支语句和逻辑运算符
这一部分截取自<C++ Primer Plus>,内容比较简单,很多只取了一些主题关键词,有空再补充: 设计智能程序的一个关键是使程序具有决策能力. 前面一种方式是循环——程序决定是否继续 ...