通知角标(2)只用一个TextView实现

 

  可以只用一个TextView实现通知角标,TextView的setCompoundDrawables函数可以在TextView的上,下,左,右,4条边处分别指定一个图片。见图1:

这个图片如果在角上,就能实现通知角标的功能。同时TextView只显示一个背景时就可当一个ImageView。

 

      图1,文本四周可以插图片

 

优点:

  不用引入其它库,或自定义一个,

缺点:

  1,角标内容只能是图片,显示1-9要用9张图,如果显示21 就比较麻烦,但是通常>10可以只显示一个小红点,或者所有通知只显示红点。

  2,当角标与文字重叠时,角标不是最上层,但角标不用必须压在文字之上。

  3,当文本居中显示时,当响应事件时要把角票设为null,这时文字会移动重新计算位置,会产生移动,可用一张透明图片占位没有通知时的状态。

 

示例:

              图2,效果图

代码:

 1   private void initNotice(){
 2         final TextView noticeView = (TextView) tabHost.findViewById(R.id.tab_weixin_notice);
 3         noticeView.setVisibility(View.VISIBLE);
 4         final Drawable left = context.getResources().getDrawable(R.drawable.tab_weixin_qq_friend);  
 5         // 这一步必须要做,否则不会显示.  
 6         left.setBounds(100, 0,175 ,74);
 7         Drawable right = context.getResources().getDrawable(R.drawable.notice_dot);
 8         right.setBounds(-120, -10,-110 ,0);
 9         noticeView.setCompoundDrawables(left,null,right,null);
10         noticeView.setOnTouchListener(new View.OnTouchListener() {
11             @Override
12             public boolean onTouch(View v, MotionEvent event) {
13                 noticeView.setCompoundDrawables(left, null, null, null);
14                 return false;
15             }
16         });
17     }

 

注意其中right的矩形的top,left如果不是0,那么要用负数,如果用right.setBounds(20,0,40,20)。那么图片显示不出来。

 

posted @ 2015-12-13 00:00  f9q  阅读(1231)  评论(0编辑  收藏  举报