卡片控件 android,Android控件CardView实现卡片布局

本文介绍了Android 5.0引入的CardView控件,它能够为FragmentLayout添加圆角和阴影效果,常用于ListView和RecyclerView的Item布局中。文章详细讲解了如何在项目中配置并使用CardView,包括设置图片圆角、阴影大小等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CardView介绍

CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果;CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为一种容器使用。CardView应该被使用在显示层次性的内容时;在显示列表或网格时更应该被选择,因为这些边缘可以使得用户更容易去区分这些内容。

使用

先看效果

994e0ca4251c95eb3e54ebdc376ff277.png

首先在build.gradle文件添加依赖库

dependencies {

compile fileTree(include: ['*.jar'], dir: 'libs')

testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:24.2.0'

compile 'com.android.support:cardview-v7:24.2.0'

}

布局文件main.html文件下

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/cardView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="10dp">

android:layout_width="match_parent"

android:layout_height="100dp">

android:layout_width="150dp"

android:layout_height="match_parent"

android:layout_margin="5dp"

android:scaleType="centerCrop"

android:src="@drawable/sng" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="5dp"

android:text="棒冰行动"

android:textSize="18sp"

android:textStyle="bold" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="5dp"

android:text="棒冰行动,公益传播设计夏令营" />

在MainActivity.java下文件

public class MainActivity extends AppCompatActivity {

private CardView cardView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

cardView = (CardView)findViewById(R.id.cardView);

cardView.setRadius(8);//设置图片圆角的半径大小

cardView.setCardElevation(8);//设置阴影部分大小

cardView.setContentPadding(5,5,5,5);//设置图片距离阴影大小

}

}

好,已结束CardView难度不大,当是实用性及及效果是非常棒的,值得你拥有!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2018-10-21

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值