Android5.0新控制項CardView的介紹和使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   使用   for   

  

CardView也是5.0的新控制項,這控制項其實就是一個卡片啦,當然我們自己也完全可以定義這樣一個卡片,從現在的微博等社App中可以看到各式各樣的自訂卡片,所以這個控制項意義不是很大。比較是support中的view所以使用在布局裡面的時候一下子看不到效果的,比較不好。CardView繼承的是FrameLayout,所以擺放內部控制項的時候需要注意一下啦。

建議:個人不是很建議用這個控制項,因為我們完全可以自訂這樣一個控制項,這樣既能在布局時直接看到效果又比較方便,關鍵問題是CardView中的很多熟悉不支援5.0之下的版本,雖然有support-v7,但也就是支援個背景和高度啥的,意義不是很大。總之呢,我們還是要看看這個CardView是個什麼東西。

 

 參數說明:

<resources>    <declare-styleable name="CardView">        <!-- Background color for CardView. -->        <!-- 背景色 -->        <attr name="cardBackgroundColor" format="color" />        <!-- Corner radius for CardView. -->        <!-- 邊緣弧度數 -->        <attr name="cardCornerRadius" format="dimension" />        <!-- Elevation for CardView. -->        <!-- 高度 -->        <attr name="cardElevation" format="dimension" />        <!-- Maximum Elevation for CardView. -->        <!-- 最大高度 -->        <attr name="cardMaxElevation" format="dimension" />        <!-- Add padding in API v21+ as well to have the same measurements with previous versions. -->        <!-- 設定內邊距,v21+的版本和之前的版本仍舊具有一樣的計算方式 -->        <attr name="cardUseCompatPadding" format="boolean" />        <!-- Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners. -->        <!-- 在v20和之前的版本中添加內邊距,這個屬性是為了防止卡片內容和邊角的重疊 -->        <attr name="cardPreventCornerOverlap" format="boolean" />        <!-- 下面是卡片邊界距離內部的距離-->        <!-- Inner padding between the edges of the Card and children of the CardView. -->        <attr name="contentPadding" format="dimension" />        <!-- Inner padding between the left edge of the Card and children of the CardView. -->        <attr name="contentPaddingLeft" format="dimension" />        <!-- Inner padding between the right edge of the Card and children of the CardView. -->        <attr name="contentPaddingRight" format="dimension" />        <!-- Inner padding between the top edge of the Card and children of the CardView. -->        <attr name="contentPaddingTop" format="dimension" />        <!-- Inner padding between the bottom edge of the Card and children of the CardView. -->        <attr name="contentPaddingBottom" format="dimension" />    </declare-styleable></resources>

 

看完了參數,那麼我們來看看布局檔案中的用法。

<!-- A CardView that contains a TextView --><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/card_view"    android:layout_width="200dp"    android:layout_height="200dp"    android:layout_gravity="center"    app:cardCornerRadius="4dp"     app:cardBackgroundColor="#ff0000"     app:cardElevation="5dp"    app:cardMaxElevation="10dp"    app:cardUseCompatPadding="true"    app:cardPreventCornerOverlap="true">    <TextView        android:id="@+id/info_text"        android:layout_width="match_parent"        android:layout_height="match_parent"         android:layout_gravity="center"        android:textSize="25sp"        android:textColor="#ffffff"        android:text="Hello CardView"/></android.support.v7.widget.CardView>

 

這樣我們就定義好了一個CardView了。本來這篇文章不需要給源碼的,但因為有人的support-v7包中這個jar有點問題(比如我),所以還是放一個代碼吧。

 

源碼下載:http://download.csdn.net/detail/shark0017/8108835

 

參考自:

http://blog.csdn.net/a396901990/article/details/40187769

 

Android5.0新控制項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.