1. 程式人生 > 程式設計 >Android控制元件CardView實現卡片效果

Android控制元件CardView實現卡片效果

這是android新推出的一個,讓卡片帶立體感的一個控制元件,就是一個卡牌,有點類似於佈局那種的東西,裡面可以新增控制元件內容

先看看執行的效果圖:

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);//設定圖片距離陰影大小
  }
}

佈局頁面:

<?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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。