Android開發--CardView使用,android--cardview

來源:互聯網
上載者:User

Android開發--CardView使用,android--cardview

Android5.0中向我們介紹了一個全新的控制項–CardView,從本質上看,可以將CardView看做是FrameLayout在自身之上添加了圓角和陰影製作效果。請注意:CardView被封裝為一種布局,並且經常在ListView和RecyclerView的Item布局中,作為一種容器使用。

CardView應該被使用在顯示層次性的內容時;在顯示列表或網格時更應該被選擇,因為這些邊緣可以使得使用者更容易去區分這些內容。

使用CardView

首先,假設你的布局如同下面的形式:

<FrameLayout    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <!-- Main Content View -->    <RelativeLayout>        ...    </RelativeLayout></FrameLayout>

為了使用上面的布局方式來建立一個卡片,首先你需要匯入支援的依賴庫(android-support-v7-cardview的jar包)在你的build.gradle檔案中。

dependencies {    ...    compile 'com.android.support:cardview-v7:21.0.2'}

現在將FrameLayout替換為CardView,

<android.support.v7.widget.CardView    xmlns:card_view="http://schemas.android.com/apk/res-auto"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <!-- Main Content View -->    <RelativeLayout>        ...    </RelativeLayout></android.support.v7.widget.CardView>

就是這樣!使用依賴庫能夠保證你的程式穩定的相容之前的版本;儘管在AndroidL和之前的Android版本中對其處理方式有所不同。

定製CardView

CardView提供了一個預設的elevation(意為CardView的Z軸陰影)和圓角角度,所以每一個卡片都能夠在不同的裝置上保持相同的外觀。然而,你也可以根據自己的需求去定製這些值。

<android.support.v7.widget.CardView    xmlns:card_view="http://schemas.android.com/apk/res-auto"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    card_view:cardCornerRadius="8dp"    card_view:cardElevation="8dp">    <!-- Main Content View -->    <RelativeLayout>        ...    </RelativeLayout></android.support.v7.widget.CardView>

注意:cardElevation屬性被用來決定陰影的大小以及柔和度,以至於可以逼真的類比出對於深度效果的描述。

添加波紋點擊效果

預設情況,CardView是不可點擊的,並且沒有任何的觸摸反饋效果。觸摸反饋動畫在使用者點擊CardView時可以給使用者以視覺上的反饋。為了實現這種行為,你必須提供一下屬性:

<android.support.v7.widget.CardView  ...  android:clickable="true"  android:foreground="?android:attr/selectableItemBackground">  ...</android.support.v7.widget.CardView>

使用android:foreground=”?android:attr/selectableItemBackground”可以使CardView點擊產生波紋的效果,有觸摸點向外擴散。

對更早的版本的支援

在AndroidL之前的裝置上,CardView為了支援圓角的效果加上了padding,圓角剪裁操作可以算是很昂貴的操作。相似的,對陰影製作效果來說,在AndroidL之前,也會提供padding去繪製陰影面積,這些內容的padding是和elevation屬性相關的,按照文檔:

padding值為:
左右兩邊的值為:maxCardElevation + (1 - cos45) * cornerRadius
上下兩邊的值為:maxCardElevation * 1.5 + (1 - cos45) * cornerRadius

因此,如果你需要給自己的內容加上padding的話,需要使用新的屬性:card_view:contentPadding
相似的,如果改變CardView的背景,也需要使用新的屬性:card_view:cardBackgroundColor

原文出處

原文來自:Using the CardView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.