android button 的点击事件

android 的button 应该是几乎每天都用到的控件了吧,但是他的点击效果很难控制,很烦。。。

先来看看官方的默认button

    <android.support.v7.widget.AppCompatButton
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:layout_marginTop="150dp" />

简书不能上传视频,只能传GIF格式的,有点烦。。
nomal_button.2018-12-06 14_52_07.gif

感觉挺好看的,有阴影,有波纹,好像还有点点击时颜色加深
但是我们一般的话都是得加个背景色,或者加个边框啥的。。

   android:background="@color/black_4A4A4A"
black_button.2018-12-06 14_50_52.gif

设置这个属性之后,就只有阴影了,没有那个波纹效果了,有点难受

还有一个属性backgroundtint 这个属性只有在21以上才能用,
看看效果:

   android:backgroundTint="@color/black_4A4A4A"
black_tint.2018-12-06 15_07_06.gif

这个有阴影和波纹 看起来挺好

那么backroundTint是个啥东西呢?
请看这个:https://blog.csdn.net/huangxiaoguo1/article/details/63282956
感兴趣的话可以去看看。

但是这个tint只能设置颜色,如果我们想做成一个圆角的button,这个方法就没用了。

是不是没办法了呢?

将就一下:
或者我们可以自己写一个波纹效果ripple。这个也是21以上才能用的。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/xxxxx">
    <item android:drawable="@drawable/xxxx" />
</ripple>

android:color 这个颜色就是波纹的颜色,

android:drawable 这个就是你的button的样子。
ripple.2018-12-06 15_35_42.gif

默认是有阴影的,不过我去掉了。。

自己写的好像不是那个好看。。

哪为了兼容4.4,只能再将就一下,点击时不要ripple了,设置两个drawable,点击的时候切换。

还有没有其他办法呢?

曾经真的以为button的点击效果就这样了,
难看的自定义波纹也只能将就了,
直到我遇到materialbutton,
让我见一次,就久久不能忘记她哪支持各种式样的波纹效果.

先来看看官方给的demo。

material.2018-12-06 16_08_06.gif
material.2018-12-06 16_10_16.gif

这样看起来没啥,主要是他的属性:

https://developer.android.com/reference/com/google/android/material/button/MaterialButton

Add icons to the start or center of this button of this button using the [app:icon], [app:iconPadding],[app:iconTint],[app:iconTintMode]and[app:iconGravity]

Specify background tint using the [app:backgroundTint]= and[app:backgroundTintMode]` attributes, which accepts either a color or a color state list.

Ripple color / press state color can be specified using the `[app:rippleColor] attribute. Ripple opacity will be determined by the Android framework when available. Otherwise, this color will be overlaid on the button at a 50% opacity when button is pressed.

Set the stroke color using the [app:strokeColor] attribute, which accepts either a color or a color state list. Stroke width can be set using the[app:strokeWidth] attribute.

Specify the radius of all four corners of the button using the [app:cornerRadius] attribute.

方法名 作用
app:icon 为button添加一个icon
app:backgroundTint和backgroundTintMode 为button添加一个背景色
app:rippleColor 设置波纹颜色
app:cornerRadius 为button设置圆角
app:strokeWidth 设置边框宽度
app:strokeColor 设置边框颜色
app:icon 为button添加一个icon

直接使用属性设置一个背景色和边框,岂不是美滋滋。。。

那就来试试吧:

官方提供来一下几种style:

    <style name="Widget.MaterialComponents.Button.Icon">
    <style name="Widget.MaterialComponents.Button.OutlinedButton">
    <style name="Widget.MaterialComponents.Button.OutlinedButton.Icon">
    <style name="Widget.MaterialComponents.Button.TextButton">
    <style name="Widget.MaterialComponents.Button.UnelevatedButton">
    <android.support.design.button.MaterialButton
        android:layout_width="150dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_height="150dp"
        android:layout_gravity="center"/>
material_outlined.2018-12-06 17_03_30.gif

看起来挺好,但是当我改变背景色的时候,问题来了。

device-2018-12-06-164539.png

仔细看边框的4个角,她处理的好像不太好,

继续变大角度

    <android.support.design.button.MaterialButton
        android:layout_width="150dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_height="150dp"
        app:backgroundTint="@color/red_dd2d34"
        app:strokeColor="@color/black_000000"
        app:strokeWidth="5dp"
        app:cornerRadius="5dp"
        android:layout_gravity="center"/>
device-2018-12-06-170756.png

为啥官方的好像没事呢?

点进去style看看就好了

  <style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:paddingLeft">@dimen/mtrl_btn_padding_left</item>
    <item name="android:paddingRight">@dimen/mtrl_btn_padding_right</item>
    <item name="strokeColor">@color/mtrl_btn_stroke_color_selector</item>
    <item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
  </style>
  <style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
    <item name="android:textColor">@color/mtrl_text_btn_text_color_selector</item>
    <item name="android:paddingLeft">@dimen/mtrl_btn_text_btn_padding_left</item>
    <item name="android:paddingRight">@dimen/mtrl_btn_text_btn_padding_right</item>
    <item name="iconTint">@color/mtrl_text_btn_text_color_selector</item>
    <item name="iconPadding">@dimen/mtrl_btn_text_btn_icon_padding</item>
    <item name="backgroundTint">@color/mtrl_btn_transparent_bg_color</item>
    <item name="rippleColor">@color/mtrl_btn_text_btn_ripple_color</item>
  </style>
    <color name="mtrl_btn_transparent_bg_color">#00ffffff</color>

默认是把button的背景设为全透明,于是我给官方demo添加一个背景色后变成这样了:

demo_black.png

哇,,,,想哭,,怎么会这样呢?

回去将就的自己写ripple吧!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,724评论 2 59
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,282评论 1 9
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,429评论 2 45
  • 不识繁花所为/ 一醉三年/ 网易云就着酒/ 也喝不出个愁滋味/ 去了浇愁心性/ 流连街头巷尾/ 路灯下乐得拖沓一地...
    沉没的鲸阅读 209评论 0 1
  • 会醉,会上瘾。 当被虫鸣鸟叫叫起的时候,以为是梦中的故乡,贪恋的又沉入梦中。 不是梦,是真实的大自然的交响...
    一米的部落格阅读 317评论 0 0