博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义显示小红点的android Button按钮
阅读量:6951 次
发布时间:2019-06-27

本文共 1926 字,大约阅读时间需要 6 分钟。

hot3.png

效果:

 

 

实现原理:

继承自Button的一个控件,小红点和数字,是直接绘制到Canvas上的,总是在右上角。之看以看起来,角标已经超出了Button,是因为在绘制Button的时候,对Canvas进行了缩放。譬如:

 

 

[html]  

  1. <com.carlos.notificatoinbutton.library.NotificationButton  
  2.     android:id="@+id/button4"  
  3.     android:layout_width="100dp"  
  4.     android:layout_height="100dp"  
  5.     android:layout_marginTop="15dp"  
  6.     android:background="@mipmap/wechat"  
  7.     notButton:circleBgColor=":color/holo_red_light"  
  8.     notButton:circleSize="12dp"  
  9.     notButton:textColor=":color/holo_green_dark" />  

这样的一个NotificationButton,设置的宽和高为100dp方形,circleSize,就是小红点的大小,为12dp。这样设置的话,实际上,你看到的Button的大小,只有88dp,是总的宽度减去小红点的大小。

 

使用方法:

在android studio中引用:

 

[plain]  

  1. compile 'com.carlos.notificatoinbutton:buttonlibrary:1.0.5'  

 

 

然后在布局文件中:

 

[html]  

  1. <com.carlos.notificatoinbutton.library.NotificationButton  
  2.     android:id="@+id/button5"  
  3.     android:layout_width="100dp"  
  4.     android:layout_height="100dp"  
  5.     android:layout_marginTop="15dp"  
  6.     android:background="@mipmap/wechat"  
  7.     notButton:circleBgColor="@android:color/holo_red_light"  
  8.     notButton:circleSize="12dp"  
  9.     notButton:textColor="@android:color/holo_green_dark" />  

因为它是继承自Button,所有Button的属性,都可以使用。除些之外,还有三个自定义属性:

 

[html]  

  1. <!--小红点的颜色-->  
  2.         notButton:circleBgColor="@android:color/holo_red_light"  
  3.         <!--小红点大小-->  
  4.         notButton:circleSize="12dp"  
  5.         <!--小红点上字的颜色-->  
  6.         notButton:textColor="@android:color/holo_green_dark"  

 

然后是在java代码中:

 

[java]  

  1. @Override  
  2.    protected void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.        setContentView(R.layout.activity_main);  
  5.        button2 = (NotificationButton) findViewById(R.id.button2);  
  6.        button3 = (NotificationButton) findViewById(R.id.button3);  
  7.        button4 = (NotificationButton) findViewById(R.id.button4);  
  8.        button5 = (NotificationButton) findViewById(R.id.button5);  
  9.        button2.setNotificationNumber(120);  
  10.        button3.setNotificationNumber(85);  
  11.        button4.setNotificationNumber(9);  
  12.        button5.setNotificationNumber(0);  
  13.    }  

只有一个方法,setNotificationNumber方法,设置了它,就可以直接显示通知数量了,

大于99的,显示99+,

小于等于0的,不显示。

 

源代码地址:

 

转载于:https://my.oschina.net/u/1177694/blog/1604924

你可能感兴趣的文章
新浪微博开放平台链接耗尽的情况分析
查看>>
极限编程的12个实践原则
查看>>
javascript--返回顶部效果
查看>>
C# NamePipe使用小结
查看>>
ZooKeeper Watcher注意事项
查看>>
Linux下清理内存和Cache方法
查看>>
表单元素的外观改变(webkit and IE10)
查看>>
帆软报表学习笔记②——行转列
查看>>
redis应用场景:实现简单计数器-防止刷单
查看>>
两款开发辅助工具介绍
查看>>
python 文件的打开与读取
查看>>
基于ROS的运动识别
查看>>
python 之selectors 实现文件上传下载
查看>>
【hdu - 2568】ACM程序设计期末考试081230
查看>>
C语言基础(一)
查看>>
python处理xml中非法字符的一种思路
查看>>
itextSharp 附pdf文件解析
查看>>
solr6.0.0 + tomcat8 配置问题
查看>>
[leetcode-303-Range Sum Query - Immutable]
查看>>
LinkButton(按钮)
查看>>