首页 > 代码库 > Android 图标上面添加提醒使用开源UI类库 Viewbadger
Android 图标上面添加提醒使用开源UI类库 Viewbadger
Viewbadger
1.BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及显示的位置等等
2.BadgeView是一个GitHub开源的项目,下载地址是:https://github.com/jgilfelt/android-viewbadger
3.这个开源项目引用的方法很简单把android-viewbadger.jar引入到项目中就可以直接使用了,它提供的Demo已经很直接把用法都写出来了,用起来很方便的,对TabHost提供了特殊的支持。
下面是提供的Demo中使用图片:
4.demo中对应的效果代码这些一看就明白了:
1 public class DemoActivity extends TabActivity { 2 3 private static final String[] DATA =http://www.mamicode.com/ Cheeses.sCheeseStrings; 4 5 Button btnPosition; 6 Button btnColour; 7 Button btnAnim1; 8 Button btnAnim2; 9 Button btnCustom; 10 Button btnClick; 11 Button btnTab; 12 Button btnIncrement; 13 14 ListView listDemo; 15 16 BadgeView badge1; 17 BadgeView badge2; 18 BadgeView badge3; 19 BadgeView badge4; 20 BadgeView badge5; 21 BadgeView badge6; 22 BadgeView badge7; 23 BadgeView badge8; 24 25 @Override 26 public void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.main); 29 30 final TabHost tabHost = getTabHost(); 31 32 tabHost.addTab(tabHost.newTabSpec("demos") 33 .setIndicator("Badge Demos") 34 .setContent(R.id.tab1)); 35 36 tabHost.addTab(tabHost.newTabSpec("adapter") 37 .setIndicator("List Adapter") 38 .setContent(R.id.tab2)); 39 40 tabHost.addTab(tabHost.newTabSpec("tests") 41 .setIndicator("Layout Tests") 42 .setContent(R.id.tab3)); 43 44 // *** default badge *** 45 46 View target = findViewById(R.id.default_target); 47 BadgeView badge = new BadgeView(this, target); 48 badge.setText("1"); 49 badge.show(); 50 51 // *** set position *** 52 53 btnPosition = (Button) findViewById(R.id.position_target); 54 badge1 = new BadgeView(this, btnPosition); 55 badge1.setText("12"); 56 badge1.setBadgePosition(BadgeView.POSITION_CENTER); 57 btnPosition.setOnClickListener(new OnClickListener() { 58 @Override 59 public void onClick(View v) { 60 badge1.toggle(); 61 } 62 }); 63 64 // *** badge/text size & colour *** 65 66 btnColour = (Button) findViewById(R.id.colour_target); 67 badge2 = new BadgeView(this, btnColour); 68 badge2.setText("New!"); 69 badge2.setTextColor(Color.BLUE); 70 badge2.setBadgeBackgroundColor(Color.YELLOW); 71 badge2.setTextSize(12); 72 btnColour.setOnClickListener(new OnClickListener() { 73 @Override 74 public void onClick(View v) { 75 badge2.toggle(); 76 } 77 }); 78 79 // *** default animation *** 80 81 btnAnim1 = (Button) findViewById(R.id.anim1_target); 82 badge3 = new BadgeView(this, btnAnim1); 83 badge3.setText("84"); 84 btnAnim1.setOnClickListener(new OnClickListener() { 85 @Override 86 public void onClick(View v) { 87 badge3.toggle(true); 88 } 89 }); 90 91 // *** custom animation *** 92 93 btnAnim2 = (Button) findViewById(R.id.anim2_target); 94 badge4 = new BadgeView(this, btnAnim2); 95 badge4.setText("123"); 96 badge4.setBadgePosition(BadgeView.POSITION_TOP_LEFT); 97 badge4.setBadgeMargin(15, 10); 98 badge4.setBadgeBackgroundColor(Color.parseColor("#A4C639")); 99 btnAnim2.setOnClickListener(new OnClickListener() {100 @Override101 public void onClick(View v) {102 TranslateAnimation anim = new TranslateAnimation(-100, 0, 0, 0);103 anim.setInterpolator(new BounceInterpolator());104 anim.setDuration(5000);105 badge4.toggle(anim, null);106 }107 });108 109 // *** custom background ***110 111 btnCustom = (Button) findViewById(R.id.custom_target);112 badge5 = new BadgeView(this, btnCustom);113 badge5.setText("37");114 badge5.setBackgroundResource(R.drawable.badge_ifaux);115 badge5.setTextSize(16);116 btnCustom.setOnClickListener(new OnClickListener() {117 @Override118 public void onClick(View v) {119 badge5.toggle(true);120 }121 });122 123 // *** clickable badge ***124 125 btnClick = (Button) findViewById(R.id.click_target);126 badge6 = new BadgeView(this, btnClick);127 badge6.setText("click me");128 badge6.setBadgeBackgroundColor(Color.BLUE);129 badge6.setTextSize(16);130 badge6.setOnClickListener(new OnClickListener() {131 @Override132 public void onClick(View v) {133 Toast.makeText(DemoActivity.this, "clicked badge", Toast.LENGTH_SHORT).show();134 }135 });136 btnClick.setOnClickListener(new OnClickListener() {137 @Override138 public void onClick(View v) {139 badge6.toggle();140 }141 });142 143 // *** tab ***144 145 TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);146 147 btnTab = (Button) findViewById(R.id.tab_btn);148 badge7 = new BadgeView(this, tabs, 0);149 badge7.setText("5");150 btnTab.setOnClickListener(new OnClickListener() {151 @Override152 public void onClick(View v) {153 badge7.toggle();154 }155 });156 157 // *** increment ***158 159 btnIncrement = (Button) findViewById(R.id.increment_target);160 badge8 = new BadgeView(this, btnIncrement);161 badge8.setText("0");162 btnIncrement.setOnClickListener(new OnClickListener() {163 @Override164 public void onClick(View v) {165 if (badge8.isShown()) {166 badge8.increment(1);167 } else {168 badge8.show();169 }170 }171 });172 173 // *** list adapter ****174 175 listDemo = (ListView) findViewById(R.id.tab2);176 listDemo.setAdapter(new BadgeAdapter(this));177 178 }179 180 @Override181 protected void onResume() {182 super.onResume();183 184 BadgeView badge;185 View target;186 187 // *** test linear layout container ***188 189 target = findViewById(R.id.linear_target);190 badge = new BadgeView(this, target);191 badge.setText("OK");192 badge.show();193 194 // *** test relative layout container ***195 196 target = findViewById(R.id.relative_target);197 badge = new BadgeView(this, target);198 badge.setText("OK");199 badge.show();200 201 // *** test frame layout container ***202 203 target = findViewById(R.id.frame_target);204 badge = new BadgeView(this, target);205 badge.setText("OK");206 badge.show();207 208 // *** test table layout container ***209 210 target = findViewById(R.id.table_target);211 badge = new BadgeView(this, target);212 badge.setText("OK");213 badge.show();214 215 // *** test linear layout ***216 217 target = findViewById(R.id.linear_group_target);218 badge = new BadgeView(this, target);219 badge.setText("OK");220 badge.show();221 222 // *** test relative layout ***223 224 target = findViewById(R.id.relative_group_target);225 badge = new BadgeView(this, target);226 badge.setText("OK");227 badge.show();228 229 // *** test frame layout ***230 231 target = findViewById(R.id.frame_group_target);232 badge = new BadgeView(this, target);233 badge.setText("OK");234 badge.show();235 236 // *** test table layout ***237 238 target = findViewById(R.id.tablerow_group_target);239 badge = new BadgeView(this, target);240 badge.setText("OK");241 badge.show();242 243 }244 245 private static class BadgeAdapter extends BaseAdapter {246 private LayoutInflater mInflater;247 private Context mContext;248 private static final int droidGreen = Color.parseColor("#A4C639");249 250 public BadgeAdapter(Context context) {251 mInflater = LayoutInflater.from(context);252 mContext = context;253 }254 255 public int getCount() {256 return DATA.length;257 }258 259 public Object getItem(int position) {260 return position;261 }262 263 public long getItemId(int position) {264 return position;265 }266 267 public View getView(int position, View convertView, ViewGroup parent) {268 ViewHolder holder;269 270 if (convertView == null) {271 convertView = mInflater.inflate(android.R.layout.simple_list_item_2, null);272 holder = new ViewHolder();273 holder.text = (TextView) convertView.findViewById(android.R.id.text1);274 holder.badge = new BadgeView(mContext, holder.text);275 holder.badge.setBadgeBackgroundColor(droidGreen);276 holder.badge.setTextColor(Color.BLACK);277 convertView.setTag(holder);278 } else {279 holder = (ViewHolder) convertView.getTag();280 }281 282 holder.text.setText(DATA[position]);283 284 if (position % 3 == 0) {285 holder.badge.setText(String.valueOf(position));286 holder.badge.show();287 } else {288 holder.badge.hide();289 }290 291 292 return convertView;293 }294 295 static class ViewHolder {296 TextView text;297 BadgeView badge;298 }299 }300 301 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。