Android 设置主题实现点击波纹效果的示例

Android 设置主题实现点击波纹效果的示例,第1张

概述开头先说说大家都知道的MaterialDesign。MaterialDesign:MaterialDesign是Google推出的一个全新的设计语言,它的特点就是拟物扁平化。

开头先说说大家都知道的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> 

自定义状态条和导航条:

material还允许你轻松的自定义状态条和导航条的颜色。

可以使用如下属性(参考下方图片):

androID:statusbarcolor,Window.setStatusbarcolor

兼容性:

由于Material theme只可以在AndroID L Developer PrevIEw中使用。

所以在低版本使用的话就需要为其另设一套主题:

在老版本使用一套主题 res/values/styles.xml,在新版本使用Material主题res/values-v21/styles.xml.

系统自带点击事件的控件一般都具有默认的波纹效果,直接使用即可:

<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中加入了触摸反馈动画。

其中最明显,最具代表性的就是波纹动画,比如当点击按钮时会从点击的位置产生类似于波纹的扩散效果。

波纹效果(Ripple):

当你使用了Material主题后,波纹动画会自动应用在所有的控件上,我们当然可以来设置其属性来调整到我们需要的效果。

可以通过如下代码设置波纹的背景:

androID:background="?androID:attr/selectableItemBackground"波纹有边界androID:background="?androID:attr/selectableItemBackgroundborderless"波纹超出边界

使用效果如下:

B1是不设任何背景的按钮

B2设置了?androID:attr/selectableItemBackground

B3设置了?androID:attr/selectableItemBackgroundborderless

设置颜色

我们也可以通过设置xml属性来调节动画颜色,从而可以适应不同的主题:

androID:colorControlHighlight:设置波纹颜色

androID:colorAccent:设置checkBox等控件的选中颜色

比如下面这个比较粉嫩的主题,就需要修改动画颜色来匹配(上面已经有介绍):

为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="待发货"           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> 

这样就可以实现波纹效果啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android 设置主题实现点击波纹效果的示例全部内容,希望文章能够帮你解决Android 设置主题实现点击波纹效果的示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1143782.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存