[Android] Android 卡片式控件CardView的優雅使用


[Android] Android 卡片式控件CardView的優雅使用

CardView是在安卓5.0提出的卡片式控件

 

其具體用法如下:

1、在app/build.gradle 文件中添加

compile 'com.android.support:cardview-v7:25.3.1' // 卡片式布局

 

2、在xml布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".modules.main.ui.MainActivity">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="20dp"
android:elevation="5dp"
app:cardBackgroundColor="#0000ff"
app:cardCornerRadius="10dp">

<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="圓角text"/>
</android.support.v7.widget.CardView>
</RelativeLayout>


3、或者 在代碼中設置使用

    cardView = (CardView)findViewById(R.id.cardView);
    cardView.setRadius(8);                    //設置圖片圓角的半徑大小
    cardView.setCardElevation(8);        //設置陰影部分大小
    cardView.setContentPadding(5,5,5,5);        //設置圖片距離陰影大小

 

另)  附上它的常見基本屬性:
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

 

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10671941.html

轉載請著名出處!謝謝~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM