温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么利用CardView控件实现卡片效果

发布时间:2021-06-26 16:39:44 来源:亿速云 阅读:262 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关Android中怎么利用CardView控件实现卡片效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1.添加依赖

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

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

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);//设置图片距离阴影大小  }}

布局页面:

<?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控件实现卡片效果有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI