Android5.0新控件實(shí)例詳解
谷歌在推出Android5.0的同時(shí)推出了一些新控件,Android5.0中最常用的新控件有下面5種。

1. CardView(卡片視圖)
CardView顧名思義是卡片視圖,它繼承FrameLayout。它是一個(gè)帶圓角的背景和陰影FrameLayout。CardView被包裝為一種布局,并且經(jīng)常在ListView和RecyclerView的Item布局中,作為容器使用。
CardView的使用非常簡(jiǎn)單:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp">
<Button
android:id="@+id/ripple_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/ripple"
android:gravity="center"
android:text="我在一個(gè)CardView里面" />
</android.support.v7.widget.CardView>

2. Patelle(調(diào)色板)
Patelle是一個(gè)輔助類,它的作用是從圖片中獲取突出的顏色。
它可以提取下面幾種特性的突出顏色:
- Vibrant(充滿活力的)
- Vibrant Dark(充滿活力,黑暗的)
- Vibrant Light(充滿活力的,明亮的)
- Muted(柔和的)
- Muted Dark(柔和的,黑暗的)
- Muted Light(柔和的,明亮的)
Patelle的使用也非常簡(jiǎn)單:
// 獲取應(yīng)用程序圖標(biāo)的Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); // 通過bitmap生成調(diào)色板palette Palette palette=Palette.from(bitmap).generate(); // 獲取palette充滿活力色顏色 int vibrantColor=palette.getVibrantColor(Color.WHITE);
3. Toolbar(工具欄)
Toolbar顧名思義是工具欄,作為ActionBar的替代品出現(xiàn),谷歌推薦使用Toolbar替代ActionBar。
Toolbar可以放置在任何地方,不像ActionBar一樣只能放置在固定的位置。
Toolbar支持比ActionBar更集中的特征。
Toolbar可能包含以下可選元素的組合:
- 導(dǎo)航按鈕
- 品牌的Logo圖像
- 標(biāo)題和子標(biāo)題
- 一個(gè)或多個(gè)自定義視圖
this.toolbar = (Toolbar) findViewById(R.id.toolbar);
this.recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
this.ripplebutton = (Button) findViewById(R.id.ripple_button);
this.button = (Button) findViewById(R.id.button);
// 設(shè)置Logo
toolbar.setLogo(R.mipmap.ic_launcher);
// 設(shè)置標(biāo)題
toolbar.setTitle("Android5.0");
// 設(shè)置子標(biāo)題
toolbar.setSubtitle("新控件");
//設(shè)置ActionBar,之后就可以獲取ActionBar并進(jìn)行操作,操作的結(jié)果就會(huì)反應(yīng)在toolbar上面
setActionBar(toolbar);
//設(shè)置了返回箭頭,,相當(dāng)于設(shè)置了toolbar的導(dǎo)航按鈕
getActionBar().setDisplayHomeAsUpEnabled(true);

4. RippleDrawable(波紋圖)
RippleDrawable顧名思義是波紋圖,只能在Android5.0以上使用,目前還沒有提供RippleDrawable向下兼容的支持包。
RippleDrawable可顯示一個(gè)漣漪效應(yīng)響應(yīng)狀態(tài)變化 。
定義一個(gè)UI的背景圖片為RippleDrawable
android:background="@drawable/ripple"
在drawable文件夾下面定義一個(gè)RippleDrawable的xml文件
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#0000FF">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="4dp" />
</shape>
</item>
</ripple>
android:color :表示波紋的顏色
<item>:表示波紋圖下面的條目
來看一下點(diǎn)擊按鈕的波紋效果

5. RecyclerView(循環(huán)視圖)
RecyclerView是ListView的替代品,谷歌推薦使用RecyclerView替代ListView。
RecyclerView提供比ListView更加靈活的使用,并且性能比ListView更優(yōu)。
RecyclerView可以設(shè)置線性,網(wǎng)格,瀑布流式三種布局管理器。
- LinearLayoutManager(線性布局管理器)
- GridLayoutManager(網(wǎng)格布局管理器)
- StaggeredGridLayoutManager(瀑布流式布局管理器)
注意:RecyclerView,Patelle,CardView是在單獨(dú)的支持包里面,不在appcompat-v7及其依賴子包中
要使用它們,必須導(dǎo)入它們的依賴包
compile 'com.android.support:recyclerview-v7:23.1.1' compile 'com.android.support:palette-v7:23.1.1' compile 'com.android.support:cardview-v7:23.1.1'
相關(guān)文章
Android中SeekBar拖動(dòng)條使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中SeekBar拖動(dòng)條使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
Android scrollToTop實(shí)現(xiàn)點(diǎn)擊回到頂部(兼容PullTorefreshScrollview)
當(dāng)頁(yè)面滑動(dòng)到底部,出現(xiàn)回到頂部的按鈕相信對(duì)大家來說并不陌生,下面這篇文章主要介紹了關(guān)于Android scrollToTop實(shí)現(xiàn)點(diǎn)擊回到頂部,并兼容PullTorefreshScrollview的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-03-03
Android編程之SurfaceView實(shí)例詳解
這篇文章主要介紹了Android編程之SurfaceView用法,簡(jiǎn)要分析了View和SurfaceView的區(qū)別,并結(jié)合實(shí)例形式分析了SurfaceView的具體使用步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽事件的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽事件的方法,結(jié)合實(shí)例形式分析了Android事件監(jiān)聽實(shí)現(xiàn)音量控制的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
基于Viewpager2實(shí)現(xiàn)登錄注冊(cè)引導(dǎo)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了基于Viewpager2實(shí)現(xiàn)登錄注冊(cè)引導(dǎo)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

