一、NumberFormat
/** * Number formatter for integers */public static final NumberFormat FORMAT_INT = NumberFormat.getIntegerInstance();textView.setText(FORMAT_INT.format(number));
/** * 格式:##.#%,小数点保留一位,0的时候不显示小数点. */public static final DecimalFormat DF = new DecimalFormat("##.#%");
二、
/** * 计算页数 * * @param total 总数 * @param count 单页数目 */public static int calculatePage(int total, int count) { if (total == 0 || count == 0) { return 0; } if (total <= count) { return 1; } int page = total / count; if (total % count > 0) { return page + 1; } else { return page; }}
三、
/** * 纯数字(需要处理空字符串). */public static boolean isDigits(String str) { return !TextUtils.isEmpty(str) && TextUtils.isDigitsOnly(str);}
/** * 绘制实心小圆点 * * @param view 宽高相等的View,默认效果是正方形 */private void drawPoint(View view) { ShapeDrawable ovalDrawable = new ShapeDrawable(new OvalShape()); ovalDrawable.getPaint().setStyle(Paint.Style.FILL); ovalDrawable.getPaint().setColor(Color.RED); view.setBackground(ovalDrawable);}
四、屏幕参数
Display display = getWindowManager().getDefaultDisplay();Point size = new Point();display.getSize(size);int width = size.x;int height = size.y;
参考StackOverflow文章:
/** * 显示一组便签,根据标签宽度自动换行。 */public class TagsLayout extends ViewGroup { private int mLineHeight; public TagsLayout(Context context) { super(context); } public TagsLayout(Context context, AttributeSet attrs) { super(context, attrs); } /** * 测量view及其内容来确定view的宽度和高度 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int width = MeasureSpec.getSize(widthMeasureSpec); // The next line is WRONG!!! Doesn't take into account requested MeasureSpec mode! int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); final int count = getChildCount(); int lineHeight = 0; int xPos = getPaddingLeft(); int yPos = getPaddingTop(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { // 用来测量出view的大小 child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED)); final int childW = child.getMeasuredWidth(); final LayoutParams lp = child.getLayoutParams(); lineHeight = Math.max(lineHeight, child.getMeasuredHeight() + lp.height); if (xPos + childW > width) { xPos = getPaddingLeft(); yPos += lineHeight; } xPos += childW + lp.width; } } mLineHeight = lineHeight; if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) { height = yPos + lineHeight; } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { if (yPos + lineHeight < height) { height = yPos + lineHeight; } } // 存储测量得到的宽度和高度值 setMeasuredDimension(width, height); } @Override protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(1, 1); // default of 1px spacing } @Override protected boolean checkLayoutParams(LayoutParams p) { return (p instanceof LayoutParams); } /** * @param changed view有新的尺寸或位置 * @param l 相对于父view的Left位置 * @param t top * @param r right * @param b bottom */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int count = getChildCount(); final int width = r - l; int xPos = getPaddingLeft(); int yPos = getPaddingTop(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final int childW = child.getMeasuredWidth(); final int childH = child.getMeasuredHeight(); final LayoutParams lp = child.getLayoutParams(); if (xPos + childW > width) { xPos = getPaddingLeft(); yPos += mLineHeight; } child.layout(xPos, yPos, xPos + childW, yPos + childH); xPos += childW + lp.width; } } }}
private boolean isEllipsized(TextView tv) { boolean flag = false; Layout l = tv.getLayout(); if (l != null) { int lines = l.getLineCount(); if (lines > 0 && l.getEllipsisCount(lines - 1) > 0) { flag = true; } } return flag;}
四、
根据页面找到代码中对应的类:连上手机adb shell dumpsys activity | grep "mResumedActivity"