Skip to content

魅族手机显示tag,圆角起始处向右偏移了一点,导致右边宽度被裁剪一点,附解决方法 #84

Description

@woshiluoyong

image

显示就是这个样子的,其他手机正常,就魅族16s pro这个手机显示异常,down下源码排查了好久,最终怀疑应该是魅族手机对tagview的测量有问题,
最后解决方法代码如下
image
image

代码也附上哈,避免github抽风图片显示不出来,其他原样无关的代码以点点点代替哈:

class TagView extends androidx.appcompat.widget.AppCompatTextView {
    .......
    public TagView(Context context, final int state, CharSequence text) {
                super(context);
                setGravity(Gravity.CENTER);
                setText(text);
                setMaxLines(1);
                setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    
                mState = state;
    
                setClickable(isAppendMode);
                setFocusable(state == STATE_INPUT);
                setFocusableInTouchMode(state == STATE_INPUT);
                setHint(state == STATE_INPUT ? inputHint : null);
                setMovementMethod(state == STATE_INPUT ? ArrowKeyMovementMethod.getInstance() : null);
    
                ........
    
                invalidatePaint();
    
                setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
    
                //setLayoutParams(new ViewGroup.LayoutParams(TagGroup.LayoutParams.WRAP_CONTENT, TagGroup.LayoutParams.WRAP_CONTENT));
                setLayoutParams(new ViewGroup.LayoutParams(StephenToolUtils.getStringPixelWidth(getPaint(), text.toString()) + (horizontalPadding * 2),
                        StephenToolUtils.getStringPixelMaxHeightForInt(getPaint()) + (verticalPadding * 2)));
      }
      ..............
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(widthSize, heightSize);
            //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        ..............
}

然后里面用到的StephenToolUtils相关方法如下:

//计算字符串的宽度(像素)
public static int getStringPixelWidth(Paint strPaint, String str) {
    return ((int) strPaint.measureText(str));
}

//计算字符串的高度(像素)
public static int getStringPixelHeight(Paint strPaint, String str) {
    Rect rect = new Rect();
    strPaint.getTextBounds(str, 0, str.length(), rect);
    return rect.height();
}

//计算字符串的高度(像素)(抵拢字体显示上下,没有空白)
public static int getStringPixelMinHeightForInt(Paint strPaint) {
    Paint.FontMetricsInt fontMetrics = strPaint.getFontMetricsInt();
    return fontMetrics.descent - fontMetrics.ascent;
}
//计算字符串的高度(像素)(字体显示上下处包含些许空白)
public static int getStringPixelMaxHeightForInt(Paint strPaint) {
    Paint.FontMetricsInt fontMetrics = strPaint.getFontMetricsInt();
    return fontMetrics.bottom - fontMetrics.top;
}

//计算字符串的高度(像素)(抵拢字体显示上下,没有空白)
public static float getStringPixelMinHeightForFloat(Paint strPaint) {
    Paint.FontMetrics fontMetrics = strPaint.getFontMetrics();
    return fontMetrics.descent - fontMetrics.ascent;
}
//计算字符串的高度(像素)(字体显示上下处包含些许空白)
public static float getStringPixelMaxHeightForFloat(Paint strPaint) {
    Paint.FontMetrics fontMetrics = strPaint.getFontMetrics();
    return fontMetrics.bottom - fontMetrics.top;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions