android自定义控件--BadgeView
来源:互联网 发布:卖旧电脑数据怎么处理 编辑:程序博客网 时间:2024/06/09 19:53
BadgeView用于在按钮或其他控件上方放置一个文字的view,类似于水果的图标数字提醒,效果图如下
注:上面这个不是我要介绍的BadgeView,是下面这个,不过效果相同,而且个人感觉更方便。
这个是我要介绍的BadgeView
直接把这个项目中的BadgeView控件抽出来放到自己的项目中拿来用就可以了
不管三七二十一,先把BadgeView的代码贴上
public class BadgeView extends TextView { private boolean mHideOnNull = true; public BadgeView(Context context) { this(context, null); } public BadgeView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public BadgeView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { if (!(getLayoutParams() instanceof LayoutParams)) { LayoutParams layoutParams = new LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.TOP); setLayoutParams(layoutParams); } // set default font setTextColor(Color.WHITE); setTypeface(Typeface.DEFAULT_BOLD); setTextSize(TypedValue.COMPLEX_UNIT_SP, 11); setPadding(dip2Px(5), dip2Px(1), dip2Px(5), dip2Px(1)); // set default background setBackground(9, Color.parseColor("#d3321b")); setGravity(Gravity.CENTER); // default values setHideOnNull(true); setBadgeCount(0); } @SuppressWarnings("deprecation") public void setBackground(int dipRadius, int badgeColor) { int radius = dip2Px(dipRadius); float[] radiusArray = new float[] { radius, radius, radius, radius, radius, radius, radius, radius }; RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null); ShapeDrawable bgDrawable = new ShapeDrawable(roundRect); bgDrawable.getPaint().setColor(badgeColor); setBackgroundDrawable(bgDrawable); } /** * @return Returns true if view is hidden on badge value 0 or null; */ public boolean isHideOnNull() { return mHideOnNull; } /** * @param hideOnNull the hideOnNull to set */ public void setHideOnNull(boolean hideOnNull) { mHideOnNull = hideOnNull; setText(getText()); } /* * (non-Javadoc) * * @see android.widget.TextView#setText(java.lang.CharSequence, android.widget.TextView.BufferType) */ @Override public void setText(CharSequence text, BufferType type) { if (isHideOnNull() && (text == null || text.toString().equalsIgnoreCase("0"))) { setVisibility(View.GONE); } else { setVisibility(View.VISIBLE); } super.setText(text, type); } public void setBadgeCount(int count) { setText(String.valueOf(count)); } public Integer getBadgeCount() { if (getText() == null) { return null; } String text = getText().toString(); try { return Integer.parseInt(text); } catch (NumberFormatException e) { return null; } } public void setBadgeGravity(int gravity) { FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams(); params.gravity = gravity; setLayoutParams(params); } public int getBadgeGravity() { FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams(); return params.gravity; } public void setBadgeMargin(int dipMargin) { setBadgeMargin(dipMargin, dipMargin, dipMargin, dipMargin); } public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) { FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams(); params.leftMargin = dip2Px(leftDipMargin); params.topMargin = dip2Px(topDipMargin); params.rightMargin = dip2Px(rightDipMargin); params.bottomMargin = dip2Px(bottomDipMargin); setLayoutParams(params); } public int[] getBadgeMargin() { FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams(); return new int[] { params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin }; } public void incrementBadgeCount(int increment) { Integer count = getBadgeCount(); if (count == null) { setBadgeCount(increment); } else { setBadgeCount(increment + count); } } public void decrementBadgeCount(int decrement) { incrementBadgeCount(-decrement); } /* * Attach the BadgeView to the TabWidget * * @param target the TabWidget to attach the BadgeView * * @param tabIndex index of the tab */ public void setTargetView(TabWidget target, int tabIndex) { View tabView = target.getChildTabViewAt(tabIndex); setTargetView(tabView); } /* * Attach the BadgeView to the target view * * @param target the view to attach the BadgeView */ public void setTargetView(View target) { if (getParent() != null) { ((ViewGroup) getParent()).removeView(this); } if (target == null) { return; } if (target.getParent() instanceof FrameLayout) { ((FrameLayout) target.getParent()).addView(this); } else if (target.getParent() instanceof ViewGroup) { // use a new Framelayout container for adding badge ViewGroup parentContainer = (ViewGroup) target.getParent(); int groupIndex = parentContainer.indexOfChild(target); parentContainer.removeView(target); FrameLayout badgeContainer = new FrameLayout(getContext()); ViewGroup.LayoutParams parentlayoutParams = target.getLayoutParams(); parentContainer.addView(badgeContainer, groupIndex, parentlayoutParams); badgeContainer.addView(target); badgeContainer.addView(this); } else if (target.getParent() == null) { Log.e(getClass().getSimpleName(), "ParentView is needed"); } } /* * converts dip to px */ private int dip2Px(float dip) { return (int) (dip * getContext().getResources().getDisplayMetrics().density + 0.5f); }}
用法:
BadgeView bv = new BadgeView(getContext());bv.setBadgeGravity(Gravity.TOP|Gravity.RIGHT);//右上角bv.setBadgeMargin(0, 8, 0, 0);//margin值bv.setBackgroundResource(R.drawable.backgroundbg);//背景bv.setText("哈哈");//文字bv.setTargetView(target);//目标view,BadgeView放置的位置
0 0
- android自定义控件--BadgeView
- BadgeView自定义数字提醒控件
- android自定义角标BadgeView
- [安卓自定义控件]BadgeView源码解析
- 【Android界面实现】信息更新小红点显示——自定义控件BadgeView的使用介绍
- android消息提示控件badgeview使用tips
- 自定义可拖拽显示数字BadgeView(仿QQ可拖拽控件)
- android BadgeView
- android badgeview
- Android 控件添加BadgeView后失去焦点问题
- Android最新消息数目提醒控件BadgeView的使用
- android自定义tabbar,并带badgeview消息提示
- GitHub 控件 BadgeView
- BadgeView控件使用教程
- 自定义Tabbar的badgeView
- 信息更新小红点显示——自定义控件BadgeView的使用介绍
- 信息更新小红点显示——自定义控件BadgeView的使用介绍
- 信息更新小红点显示——自定义控件BadgeView的使用介绍
- DNSPOD使用小结
- VC2010开发MFC ActiveX控件基础教程
- SNMP--How to install SNMP extension agent DLL(如何安装SNMP扩展代理DLL)
- 高通camera流程
- 《Architecture of a Database System》中文版
- android自定义控件--BadgeView
- 整理网站负载均衡架构
- HDU 3367 Pseudoforest 并查集求最大生成树及判断环
- UML建模语言进阶 - 用例视图详解 用例视图建模实战
- Sicily1345(能量项链DP)
- Mysql常用命令行大全
- Generating SSH Keys
- HTML中meta标签使用简介
- 服务器并发数和访问量计算与服务器配置