自定义显示小红点的android Button按钮
2016-03-12 11:03
585 查看
效果:
实现原理:
继承自Button的一个控件,小红点和数字,是直接绘制到Canvas上的,总是在右上角。之看以看起来,角标已经超出了Button,是因为在绘制Button的时候,对Canvas进行了缩放。譬如:<com.carlos.notificatoinbutton.library.NotificationButton
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp"
android:background="@mipmap/wechat"
notButton:circleBgColor="@android:color/holo_red_light"
notButton:circleSize="12dp"
notButton:textColor="@android:color/holo_green_dark" />
这样的一个NotificationButton,设置的宽和高为100dp方形,circleSize,就是小红点的大小,为12dp。这样设置的话,实际上,你看到的Button的大小,只有88dp,是总的宽度减去小红点的大小。使用方法:
在android studio中引用:compile 'com.carlos.notificatoinbutton:buttonlibrary:1.0.5'
然后在布局文件中:
<com.carlos.notificatoinbutton.library.NotificationButton android:id="@+id/button5" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="15dp" android:background="@mipmap/wechat" notButton:circleBgColor="@android:color/holo_red_light" notButton:circleSize="12dp" notButton:textColor="@android:color/holo_green_dark" />
因为它是继承自Button,所有Button的属性,都可以使用。除些之外,还有三个自定义属性:
<!--小红点的颜色--> notButton:circleBgColor="@android:color/holo_red_light" <!--小红点大小--> notButton:circleSize="12dp" <!--小红点上字的颜色--> notButton:textColor="@android:color/holo_green_dark"
然后是在java代码中:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button2 = (NotificationButton) findViewById(R.id.button2); button3 = (NotificationButton) findViewById(R.id.button3); button4 = (NotificationButton) findViewById(R.id.button4); button5 = (NotificationButton) findViewById(R.id.button5); button2.setNotificationNumber(120); button3.setNotificationNumber(85); button4.setNotificationNumber(9); button5.setNotificationNumber(0); }
只有一个方法,setNotificationNumber方法,设置了它,就可以直接显示通知数量了,
大于99的,显示99+,
小于等于0的,不显示。
源代码地址:
https://github.com/ws123/NotificationButton
相关文章推荐
- android的消息处理机制
- Android jni学习
- 无知记事本(android版)
- Android:Activity四种启动模式
- android初次学习(android studio安装及注意事项 1.5版本)
- Android笔记--简单的Fragment和Activity的之间的数据交互(传值)
- Android TTS说中文
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条
- 安卓虚拟机启动后报错: 类似 SDK Manager] Error: Error parsing .....devices.xml 解决方案
- android: PendingIntent的使用
- Android按钮单击事件的四种常用写法总结
- android生命周期理解
- AndroidStudio添加assets目录并获取其路径
- Android Studio - 使用第三方库
- qt for Android 环境搭建 win10
- Android 热修复使用Gradle Plugin1.5改造Nuwa插件
- android ListView 中CheckBox选中后,状态混乱的解决方案
- 理解Android自定义的属性
- Fragment向activity传值的接口回调方法
- 【开源-Android】基于蓝牙备份联系人