Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android CardView 詳解與使用方法

編輯:關於 Android 程式設計

CardView 是 Android 5.0 之後常被用來建立卡片式界面的元件。簡單來說,它像是在 FrameLayout 上加入圓角與陰影效果,因此很適合用於列表項、資訊卡片或內容容器。

基本屬性

CardView 常用屬性包含:

cardBackgroundColor:設定卡片背景色。
cardCornerRadius:設定圓角大小。
cardElevation:設定陰影高度。
cardUseCompatPadding:是否使用相容 padding。
contentPadding:設定內容內距。

XML 使用方式

在配置檔中加入 CardView,並放入 TextView、ImageView 等子元件即可。

<android.support.v7.widget.CardView
    xmlns:card_view="https://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="#303069"
    card_view:cardCornerRadius="10dp"
    card_view:contentPadding="10dp">

    <TextView
        android:text="我是 CardView"
        android:textColor="#fff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v7.widget.CardView>

由於 CardView 本質上接近 FrameLayout,所以使用方式也與 FrameLayout 類似,只是多了卡片外觀相關屬性。

如果需要圓角卡片效果,使用 CardView 通常會比自行處理剪裁與陰影更直觀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved