Android 設置主題實現(xiàn)點擊波紋效果的示例
開頭先說說大家都知道的Material Design。
Material Design:
Material Design是Google推出的一個全新的設計語言,它的特點就是擬物扁平化。
Material Design包含了很多內容,我大致把它分為四部分:
- 主題和布局
- 視圖和陰影
- UI控件
- 動畫
Material Theme
使用Material主題:
Material主題只能應用在Android L版本。
應用Material主題很簡單,只需要修改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中直接設置主題:
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)條和導航條:
material還允許你輕松的自定義狀態(tài)條和導航條的顏色。
可以使用如下屬性(參考下方圖片):
android:statusBarColor,Window.setStatusBarColor
兼容性:
由于Material Theme只可以在Android L Developer Preview中使用。
所以在低版本使用的話就需要為其另設一套主題:
在老版本使用一套主題 res/values/styles.xml,在新版本使用Material主題res/values-v21/styles.xml.
系統(tǒng)自帶點擊事件的控件一般都具有默認的波紋效果,直接使用即可:
<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添加點擊波紋效果呢,先了解下面的東西。
觸摸反饋:
在Android L5.0中加入了觸摸反饋動畫。
其中最明顯,最具代表性的就是波紋動畫,比如當點擊按鈕時會從點擊的位置產(chǎn)生類似于波紋的擴散效果。
波紋效果(Ripple):
當你使用了Material主題后,波紋動畫會自動應用在所有的控件上,我們當然可以來設置其屬性來調整到我們需要的效果。
可以通過如下代碼設置波紋的背景:
android:background="?android:attr/selectableItemBackground"波紋有邊界 android:background="?android:attr/selectableItemBackgroundBorderless"波紋超出邊界
使用效果如下:
B1是不設任何背景的按鈕
B2設置了?android:attr/selectableItemBackground
B3設置了?android:attr/selectableItemBackgroundBorderless
設置顏色
我們也可以通過設置xml屬性來調節(jié)動畫顏色,從而可以適應不同的主題:
android:colorControlHighlight:設置波紋顏色
android:colorAccent:設置checkbox等控件的選中顏色
比如下面這個比較粉嫩的主題,就需要修改動畫顏色來匹配(上面已經(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>
為Textview添加波紋效果:
<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>
這樣就可以實現(xiàn)波紋效果啦!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android服務應用ClockService實現(xiàn)鬧鐘功能
這篇文章主要為大家詳細介紹了Android服務應用ClockService實現(xiàn)鬧鐘功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11Android實現(xiàn)recyclerview城市字母索引列表
大家好,本篇文章主要講的是Android實現(xiàn)recyclerview城市字母索引列表,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Android入門之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細介紹了Android如何使用SharedPreference實現(xiàn)存取信息,文中的示例代碼講解詳細,對我們學習Android有一定的幫助,需要的可以參考一下2022-12-12android使用FlipAnimation實現(xiàn)3D垂直翻轉動畫
這篇文章主要為大家詳細介紹了android使用FlipAnimation實現(xiàn)3D垂直翻轉動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android百度地圖實現(xiàn)搜索和定位及自定義圖標繪制并點擊時彈出泡泡
這篇文章主要介紹了Android百度地圖實現(xiàn)搜索和定位及自定義圖標繪制并點擊時彈出泡泡的相關資料,需要的朋友可以參考下2016-01-01