在做app的时候,想要将提醒内容在按钮上显示出来,就像qq上有多少条未读消息的提醒就像图中显示的内容;
构造方法:
主要方法:
除了父类TextView本身的方法以外,子类的方法主要在于设置label的属性,包括显示位置,背景图,背景颜色,动画效果等;
Toggle the badge visibility in the UI.
setXXX方法:
setBadgePosition(); //设置显示位置;
setBadgeMargin(); //设置边距
setBadgeBackgroundColor(); //设置背景颜色
Make the badge non-visible in the UI.
Make the badge visible in the UI.
Toggle方法是显示和隐藏的,可以带进入和退出的动画效果;
Hide和Show也分别是隐藏和显示的方法,同样可以带进入和退出的动画效果;动画效果可以自定义,使用比较灵活简便。
使用如:
ImageView target= (ImageView )findViewById(R.id.imageview);
BadgeView bv = new BadgeView(this, target);
bv.setText(“Hello World!”);
bv.setTextColor(Color.Yellow);
bv.setTextSize(12);
bv.setBadgePosition(BadgeView.POSITION_TOP_RIGHT); //默认值
bv.show();
用法很简单,具体可参考BadgeView.Java。
写博客只是为了积累自己的技术并想让自己坚持长久的做一件事情,算是对程序员组织、总结能力的一种积累,粗浅执笔,如有不当之处,还请多指教!!!
Android-badgeview源代码下载路径:https://github.com/jgilfelt/android-viewbadger
但是在用的过程中,我用的imagebutton显示布局,结果在测试后,发现提醒的数字显示出来了,但是将我的imagebutton中的图片挤掉了 ,这个就开始找问题了 找了很久没有造出来,然后我做了测试
这是我原先的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<ImageButton
android:layout_marginBottom="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:background="#ffffff"
android:src="@drawable/ib_maintain_apply"
android:textSize="12sp"
android:id="@+id/ib"
android:layout_width="0dp"
android:layout_height="100dp"
android:text="维修结单"
android:layout_weight="1"
/>
<ImageButton
android:layout_marginBottom="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:background="#ffffff"
android:src="@drawable/ib_maintain_apply"
android:textSize="12sp"
android:id="@+id/ib2"
android:layout_width="0dp"
android:layout_height="100dp"
android:text="维修结单"
android:layout_weight="1"
/>
</LinearLayout>
这样的布局显示不出imagebutton的图片 在我的测试中发现显示不出图片的原因是我的width=“0dp”,这是我个人的一个写法,习惯在weight中使用0dp的写法,这样造成了imagebutton的图片显示不出来,后来我又试了 android:layout_width="wrap_content",发现还是不行的,至于什么原因我还在研究中,这些都不行我开始试了android:layout_width="fill_parent",发现这样做正好达到了我的要求 ,当然在布局中如果将button的布局height,和width 设置成固定数据,这样在显示的时候也不会出行将imagebutton的背景挤掉的情况,因为这个提醒,会占用布局。
写的这些我都感觉非常的不通顺,这是我初次写的,还有很多不足的地方,希望各位能多多包涵,多多指教。