我是靠谱客的博主 眯眯眼墨镜,这篇文章主要介绍Android控件CardView实现卡片效果,现在分享给大家,希望可以做个参考。

这是android新推出的一个,让卡片带立体感的一个控件,就是一个卡牌,有点类似于布局那种的东西,里面可以添加控件内容

先看看运行的效果图:

1.添加依赖

复制代码
1
implementation 'com.android.support:cardview-v7:25.3.1'

2.主界面设置一些卡片的属性:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.admin.ztest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.CardView; /* app:cardBackgroundColor这是设置背景颜色 app:cardCornerRadius这是设置圆角大小 app:cardElevation这是设置z轴的阴影 app:cardMaxElevation这是设置z轴的最大高度值 app:cardUseCompatPadding是否使用CompatPadding app:cardPreventCornerOverlap是否使用PreventCornerOverlap app:contentPadding 设置内容的padding app:contentPaddingLeft 设置内容的左padding app:contentPaddingTop 设置内容的上padding app:contentPaddingRight 设置内容的右padding app:contentPaddingBottom 设置内容的底padding */ public class MainActivity extends AppCompatActivity { CardView cardView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); cardView = (CardView) findViewById(R.id.cardView); cardView.setRadius(8);//设置图片圆角的半径大小 cardView.setCardElevation(8);//设置阴影部分大小 cardView.setContentPadding(5, 5, 5, 5);//设置图片距离阴影大小 } }

布局页面:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/cardView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="600pt" android:layout_height="100pt" android:background="@drawable/bg_battery_detail" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:background="#184467"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="horizontal"> <TextView android:id="@+id/tvBatteryNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="电池编号" android:textColor="@color/color_z2" android:textSize="20sp" /> <TextView android:id="@+id/tvBatterySlotNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15pt" android:text="@string/app_name" android:textColor="@color/white" android:textSize="20sp" /> </LinearLayout> <ImageView android:id="@+id/ivCloseDialog" android:layout_width="39pt" android:layout_height="39pt" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="8pt" android:padding="7pt" android:src="@mipmap/popup_del" /> </RelativeLayout> </LinearLayout> </android.support.v7.widget.CardView>

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

最后

以上就是眯眯眼墨镜最近收集整理的关于Android控件CardView实现卡片效果的全部内容,更多相关Android控件CardView实现卡片效果内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(144)

评论列表共有 0 条评论

立即
投稿
返回
顶部