還記得我們一共學過了多少UI控件了嗎?都掌握的怎麼樣啊。
安卓中一些常用控件學習得差不多了,今天再來學習一個新的控件CardView,在實際開發中也有非常高的地位。
一、CardView簡介
CardView是Android 5.0系統引入的控件,相當於FragmentLayout佈局控件然後添加圓角及陰影的效果。
CardView繼承自Framelayout,所以FrameLayout所有屬性CardView均可以直接拿來用,不過CardView還有自己獨有的屬性,常用屬性如下:
-
app:cardElevation:設置陰影的大小。
-
app:cardMaxElevation:設置陰影最大高度。
-
app:cardBackgroundColor:設置卡片的背景色。
-
app:cardCornerRadius:設置卡片的圓角大小。
-
app:contentPadding:設置內容的padding。
-
app:contentPaddingTop:設置內容的上padding。
-
app:contentPaddingLeft:設置內容的左padding。
-
app:contentPaddingRight:設置內容的右padding。
-
app:contentPaddingBottom:設置內容的底padding。
-
app:cardUseCompatPadding:是否使用CompatPadding。
-
app:cardPreventConrerOverlap:是否使用PreventCornerOverlap。
這裏有一點需要值得注意,之前學習到的控件屬性都是android:開頭的,而這裏所列的屬性是app:開頭的,如果繼續使用默認的會提示找不見對應屬性,需要我們定義一個app命名空間,在佈局文件中需要加入xmlns:app="http://schemas.android.com/apk/res-auto"語句,具體見後續案例,這裏不作過多介紹,後續再詳細學習。
二、CardView示例1
接下來通過幾個簡單的小示例程序來進一步學習CardView。
繼續使用WidgetSample工程的advancedviewsample模塊,首先需要添加支持庫,具體操作步驟同之前分享的揭開RecyclerView廬山真面目,這裏不再重複分享。這次輸入的關鍵字是cardview,即可完成CardView依賴庫的添加。
在src/main/res/layout/目錄下創建cardview_layout.xml文件,在其中填充如下代碼片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="正常使用效果"
android:gravity="center_horizontal|center_vertical"
android:textSize="20sp"
android:padding="10dp"
android:layout_margin="10dp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#669900"
app:cardCornerRadius="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="設置背景和標籤"
android:gravity="center_horizontal|center_vertical"
android:textSize="20sp"
android:padding="20dp"
android:layout_margin="10dp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#874528"
app:cardElevation="20dp"
app:cardCornerRadius="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="設置陰影"
android:gravity="center_horizontal|center_vertical"
android:textSize="20sp"
android:padding="10dp"
android:layout_margin="10dp"/>
</android.support.v7.widget.CardView>
</LinearLayout>
然後新建CardViewActivity.java文件,加載上面的佈局文件,填充的代碼如下:
public class CardViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardview_layout);
}
}
修改啓動的Activity,運行程序可以看到下圖所示效果。
三、CardView示例2
CardView被包裝爲一種佈局,並且經常在ListView和RecyclerView的Item佈局中,作爲一種容器使用。CardView應該被使用在顯示層次性的內容時;在顯示列表或網格時更應該被選擇,因爲這些邊緣可以使得用戶更容易去區分這些內容。
接下來簡單定義一個CardView的item項,並在Java代碼中修改CardView的屬性,關於結合ListView和RecyclerView的部分比較簡單,這裏不做過多介紹。
繼續再上一個案例的基礎上進行修改,修改後的cardview_layout.xml文件代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="15dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@drawable/image_01"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:gravity="right|bottom"
android:text="CardView作爲item使用"
android:textColor="@android:color/white"
android:textSize="24sp"/>
</android.support.v7.widget.CardView>
繼續修改CardViewActivity.java文件,獲得CardView組件並動態修改其屬性,修改後的代碼如下:
package com.jinyu.cqkxzsxy.android.advancedviewsample;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
/**
* @創建者 鑫鱻
* @描述 Android零基礎入門到精通系列教程,歡迎關注微信公衆號ShareExpert
*/
public class CardViewActivity extends AppCompatActivity {
private CardView mCardView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardview_layout);
mCardView = (CardView) findViewById(R.id.cardview);
// 設置卡片圓角的半徑大小
mCardView.setRadius(20);
// 設置卡片背景的顏色
mCardView.setCardBackgroundColor(Color.RED);
// 設置陰影部分大小
mCardView.setCardElevation(10);
// 設置卡片距離陰影大小
mCardView.setContentPadding(10, 10, 10, 10);
}
}
重新運行程序,可以得到下圖所示效果。
至此,CardView的學習到此告一段落,是不是發現使用起來也非常簡單,更多用法建議自己去摸索。
今天就先到這裏,如果有問題歡迎留言一起探討,也歡迎加入Android零基礎入門技術討論微信羣,共同成長!
此文章版權爲微信公衆號分享達人秀(ShareExpert)——鑫鱻所有,若需轉載請聯繫作者授權,特此聲明!
往期總結分享:
Android零基礎入門第1節:Android的前世今生
Android零基礎入門第2節:Android 系統架構和應用組件那些事
Android零基礎入門第3節:帶你一起來聊一聊Android開發環境
Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招
Android零基礎入門第5節:善用ADT Bundle, 輕鬆邂逅女神
Android零基礎入門第6節:配置優化SDK Manager, 正式約會女神
Android零基礎入門第7節:搞定Android模擬器,開啓甜蜜之旅
Android零基礎入門第8節:HelloWorld,我的第一趟旅程出發點
Android零基礎入門第9節:Android應用實戰,不懂代碼也可以開發
Android零基礎入門第10節:開發IDE大升級,終於迎來了Android Studio
Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程
Android零基礎入門第12節:熟悉Android Studio界面,開始裝逼賣萌
Android零基礎入門第13節:Android Studio個性化配置,打造開發利器
Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代
Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航
Android零基礎入門第16節:Android用戶界面開發概述
Android零基礎入門第17節:文本框TextView
Android零基礎入門第18節:輸入框EditText
Android零基礎入門第19節:按鈕Button
Android零基礎入門第20節:複選框CheckBox和單選按鈕RadioButton
Android零基礎入門第21節:開關組件ToggleButton和Switch
Android零基礎入門第22節:圖像視圖ImageView
Android零基礎入門第23節:圖像按鈕ImageButton和縮放按鈕ZoomButton
Android零基礎入門第24節:自定義View簡單使用,打造屬於你的控件
Android零基礎入門第25節:簡單且最常用的LinearLayout線性佈局
Android零基礎入門第26節:兩種對齊方式,layout_gravity和gravity大不同
Android零基礎入門第27節:正確使用padding和margin
Android零基礎入門第28節:輕鬆掌握RelativeLayout相對佈局
Android零基礎入門第29節:善用TableLayout表格佈局
Android零基礎入門第30節:兩分鐘掌握FrameLayout幀佈局
Android零基礎入門第31節:少用的AbsoluteLayout絕對佈局
Android零基礎入門第32節:新推出的GridLayout網格佈局
Android零基礎入門第33節:Android事件處理概述
Android零基礎入門第34節:Android中基於監聽的事件處理
Android零基礎入門第35節:Android中基於回調的事件處理
Android零基礎入門第36節:Android系統事件的處理
Android零基礎入門第37節:初識ListView
Android零基礎入門第38節:初識Adapter
Android零基礎入門第39節:ListActivity和自定義列表項
Android零基礎入門第40節:自定義ArrayAdapter
Android零基礎入門第41節:使用SimpleAdapter
Android零基礎入門第42節:自定義BaseAdapter
Android零基礎入門第43節:ListView優化和列表首尾使用
Android零基礎入門第44節:ListView數據動態更新
Android零基礎入門第45節:網格視圖GridView
Android零基礎入門第46節:列表選項框Spinner
Android零基礎入門第47節:自動完成文本框AutoCompleteTextView
Android零基礎入門第48節:可摺疊列表ExpandableListView
Android零基礎入門第49節:AdapterViewFlipper圖片輪播
Android零基礎入門第50節:StackView卡片堆疊
Android零基礎入門第51節:進度條ProgressBar
Android零基礎入門第52節:自定義ProgressBar炫酷進度條
Android零基礎入門第53節:拖動條SeekBar和星級評分條RatingBar
Android零基礎入門第54節:視圖切換組件ViewSwitcher
Android零基礎入門第55節:ImageSwitcher和TextSwitcher
Android零基礎入門第56節:翻轉視圖ViewFlipper
Android零基礎入門第57節:DatePicker和TimePicker選擇器
Android零基礎入門第58節:數值選擇器NumberPicker
Android零基礎入門第59節:常用三大Clock時鐘組件
Android零基礎入門第60節:日曆視圖CalendarView和定時器Chronometer
Android零基礎入門第61節:滾動視圖ScrollView
Android零基礎入門第62節:搜索框組件SearchView
Android零基礎入門第63節:值得借鑑學習的選項卡TabHost
Android零基礎入門第64節:揭開RecyclerView廬山真面目
Android零基礎入門第65節:RecyclerView分割線開發技巧
Android零基礎入門第66節:RecyclerView點擊事件處理
Android零基礎入門第67節:RecyclerView數據動態更新
Android零基礎入門第68節:RecyclerView添加首尾視圖
Android零基礎入門第69節:ViewPager快速實現引導頁
Android零基礎入門第70節:ViewPager打造TabHost效果