Android 設(shè)置主題實(shí)現(xiàn)點(diǎn)擊波紋效果的示例
開(kāi)頭先說(shuō)說(shuō)大家都知道的Material Design。
Material Design:
Material Design是Google推出的一個(gè)全新的設(shè)計(jì)語(yǔ)言,它的特點(diǎn)就是擬物扁平化。
Material Design包含了很多內(nèi)容,我大致把它分為四部分:
- 主題和布局
- 視圖和陰影
- UI控件
- 動(dòng)畫(huà)
Material Theme
使用Material主題:
Material主題只能應(yīng)用在Android L版本。
應(yīng)用Material主題很簡(jiǎn)單,只需要修改res/values/styles.xml文件,使其繼承android:Theme.Material。如下:
<!-- res/values/styles.xml --> <resources> <!-- your app's theme inherits from the Material theme --> <style name="AppTheme" parent="android:Theme.Material"> <!-- theme customizations --> </style> </resources>
或者在AndroidManifest.xml中直接設(shè)置主題:
android:theme="@android:style/Theme.Material.Light"
在最新的5.0中,google似乎不推薦使用Material Design主題了,而是由AppCompat代替。
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
自定義狀態(tài)條和導(dǎo)航條:
material還允許你輕松的自定義狀態(tài)條和導(dǎo)航條的顏色。
可以使用如下屬性(參考下方圖片):
android:statusBarColor,Window.setStatusBarColor
兼容性:
由于Material Theme只可以在Android L Developer Preview中使用。
所以在低版本使用的話(huà)就需要為其另設(shè)一套主題:
在老版本使用一套主題 res/values/styles.xml,在新版本使用Material主題res/values-v21/styles.xml.
系統(tǒng)自帶點(diǎn)擊事件的控件一般都具有默認(rèn)的波紋效果,直接使用即可:
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/myBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawablePadding="10dp" android:gravity="center_vertical" android:paddingBottom="15dip" android:paddingLeft="15dip" android:paddingRight="25dip" android:paddingTop="15dip" android:text="Click" android:textColor="@color/common_black_text" android:textSize="16sp" /> </RelativeLayout>
怎么為view添加點(diǎn)擊波紋效果呢,先了解下面的東西。
觸摸反饋:
在Android L5.0中加入了觸摸反饋動(dòng)畫(huà)。
其中最明顯,最具代表性的就是波紋動(dòng)畫(huà),比如當(dāng)點(diǎn)擊按鈕時(shí)會(huì)從點(diǎn)擊的位置產(chǎn)生類(lèi)似于波紋的擴(kuò)散效果。
波紋效果(Ripple):
當(dāng)你使用了Material主題后,波紋動(dòng)畫(huà)會(huì)自動(dòng)應(yīng)用在所有的控件上,我們當(dāng)然可以來(lái)設(shè)置其屬性來(lái)調(diào)整到我們需要的效果。
可以通過(guò)如下代碼設(shè)置波紋的背景:
android:background="?android:attr/selectableItemBackground"波紋有邊界 android:background="?android:attr/selectableItemBackgroundBorderless"波紋超出邊界
使用效果如下:
B1是不設(shè)任何背景的按鈕
B2設(shè)置了?android:attr/selectableItemBackground
B3設(shè)置了?android:attr/selectableItemBackgroundBorderless
設(shè)置顏色
我們也可以通過(guò)設(shè)置xml屬性來(lái)調(diào)節(jié)動(dòng)畫(huà)顏色,從而可以適應(yīng)不同的主題:
android:colorControlHighlight:設(shè)置波紋顏色
android:colorAccent:設(shè)置checkbox等控件的選中顏色
比如下面這個(gè)比較粉嫩的主題,就需要修改動(dòng)畫(huà)顏色來(lái)匹配(上面已經(jīng)有介紹):
為view添加波紋效果:
<RelativeLayout android:id="@+id/user_info_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:background="?android:attr/selectableItemBackground" android:layout_marginTop="10dp" android:paddingBottom="15dip" android:paddingTop="15dip"> <TextView android:id="@+id/user_info_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="10dp" android:gravity="center_vertical" android:paddingLeft="15dip" android:paddingRight="25dip" android:text="我的資料" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:contentDescription="@null" android:paddingRight="15dip" /> </RelativeLayout>
為T(mén)extview添加波紋效果:
<LinearLayout android:layout_width="match_parent" android:layout_height="68dp" android:weightSum="4" android:gravity="center_vertical"> <TextView android:id="@+id/user_unpaid" android:layout_width="0dp" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackgroundBorderless" android:drawableTop="@mipmap/ic_user_paid" android:drawablePadding="5dp" android:gravity="center" android:layout_weight="1" android:text="待付款" android:textSize="12sp" android:clickable="true"/> <TextView android:id="@+id/user_paid" android:layout_width="0dp" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackgroundBorderless" android:drawableTop="@mipmap/ic_user_paid" android:drawablePadding="5dp" android:layout_weight="1" android:gravity="center" android:text="待發(fā)貨" android:textColor="@color/common_black_text" android:textSize="12sp" android:clickable="true"/> <TextView android:id="@+id/user_unreceived" android:layout_width="0dp" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackgroundBorderless" android:drawableTop="@mipmap/ic_user_paid" android:drawablePadding="5dp" android:gravity="center" android:layout_weight="1" android:text="待收貨" android:textColor="@color/common_black_text" android:textSize="12sp" android:clickable="true"/> <TextView android:id="@+id/user_completed" android:layout_width="0dp" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackgroundBorderless" android:drawableTop="@mipmap/ic_user_paid" android:drawablePadding="5dp" android:gravity="center" android:layout_weight="1" android:text="已完成" android:textSize="12sp" android:clickable="true"/> </LinearLayout>
這樣就可以實(shí)現(xiàn)波紋效果啦!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android主題切換之探究白天和夜間模式
- 基于android樣式與主題(style&theme)的詳解
- Android中應(yīng)用界面主題Theme使用方法和頁(yè)面定時(shí)跳轉(zhuǎn)應(yīng)用
- 分析Android多主題顏色的相關(guān)問(wèn)題
- Android入門(mén)教程之創(chuàng)建樣式與主題
- Android編程應(yīng)用風(fēng)格和主題詳解
- android仿iphone主題效果的主菜單
- Android實(shí)現(xiàn)換膚的兩種思路分析
- Android編程實(shí)現(xiàn)換膚功能實(shí)例
- Android開(kāi)發(fā)實(shí)現(xiàn)切換主題及換膚功能示例
相關(guān)文章
ASM的tree?api對(duì)匿名線(xiàn)程的hook操作詳解
這篇文章主要為大家介紹了ASM的tree?api對(duì)匿名線(xiàn)程的hook操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Android實(shí)現(xiàn)recyclerview城市字母索引列表
大家好,本篇文章主要講的是Android實(shí)現(xiàn)recyclerview城市字母索引列表,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2022-01-01android 獲取視頻,圖片縮略圖的具體實(shí)現(xiàn)
android 獲取視頻,圖片縮略圖的具體實(shí)現(xiàn),需要的朋友可以參考一下2013-06-06Android入門(mén)之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細(xì)介紹了Android如何使用SharedPreference實(shí)現(xiàn)存取信息,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12Android編寫(xiě)文件瀏覽器簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android編寫(xiě)文件瀏覽器簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android實(shí)現(xiàn)畫(huà)板功能(一)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)畫(huà)板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
這篇文章主要介紹了Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡的相關(guān)資料,需要的朋友可以參考下2016-01-01