public class NewTextView extends android.support.v7.widget.AppCompatTextView {
public NewTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = (int) Math.ceil(getMaxLineWidth(getLayout()));
int height = getMeasuredHeight();
setMeasuredDimension(width, height);
}
private float getMaxLineWidth(Layout layout) {
float maximumWidth = 0.0f;
int lines = layout.getLineCount();
for (int i = 0; i < lines; i++) {
maximumWidth = Math.max(layout.getLineWidth(i), maximumWidth);
}
return maximumWidth;
}
}
但是底下回复:
For API - 23 and above you can use android:breakStrategy=“simple” 不能解决。