[Android] TextViewの自動改行テキストレイアウトが不均一



Textview Automatic Line Break Text Layout Is Uneven



2019 Unicorn Enterprise Heavy Gold Recruitment Python Engineer Standard >>> hot3.png

参照オンライン記事:



http://www.2cto.com/kf/201503/383397.html

http://www.apkbus.com/android-176726-1-1.html



public class CYTextView extends TextView { Public static int m_iTextHeight // height of the text Public static int m_iTextWidth// width of the text private Paint mPaint = null private String string = '' Private float LineSpace = 0// line spacing private Context context public CYTextView(Context context, AttributeSet set) { super(context, set) this.context = context TypedArray typedArray = context.obtainStyledAttributes(set, R.styleable.CYTextView) float textsize = typedArray.getDimension(R.styleable.CYTextView_mytextSize, 20) int textcolor = typedArray.getColor(R.styleable.CYTextView_mytextColor, -1442840576) float linespace = typedArray.getDimension(R.styleable.CYTextView_lineSpacingExtra, -12) int typeface = typedArray.getColor(R.styleable.CYTextView_typeface, 0) linespace = DensityUtil.dip2px(context, -5) typedArray.recycle() // Set the width and line spacing of CY TextView www.linuxidc.com LineSpace = linespace / / Build paint object mPaint = new Paint() mPaint.setAntiAlias(true) mPaint.setColor(textcolor) mPaint.setTextSize(textsize) switch (typeface) { case 0: mPaint.setTypeface(Typeface.DEFAULT) break case 1: mPaint.setTypeface(Typeface.SANS_SERIF) break case 2: mPaint.setTypeface(Typeface.SERIF) break case 3: mPaint.setTypeface(Typeface.MONOSPACE) break default: mPaint.setTypeface(Typeface.DEFAULT) break } } @Override public void setMaxLines(int maxlines) { super.setMaxLines(maxlines) } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas) if (m_iTextWidth <= 0) { return } char ch int w = 0 int istart = 0 int m_iFontHeight int m_iRealLine = 0 Int x = DensityUtil.dip2px(context, 20) //60 //distance from left int y = 10 Vector m_String = new Vector() FontMetrics fm = mPaint.getFontMetrics() m_iFontHeight = (int) Math.ceil(fm.descent - fm.top) + (int) LineSpace// Calculate font height (font height + line spacing) y = (int) Math.ceil(fm.descent - fm.top) for (int i = 0 i m_iTextWidth) { m_iRealLine++ m_String.addElement(string.substring(istart, i)) istart = i i-- w = 0 } else { if (i == (string.length() - 1)) { m_iRealLine++ m_String.addElement(string.substring(istart, string.length())) } } } } /** Here is the requirement requirement 2 lines */ if (m_iRealLine > 2){ m_iRealLine = 2 } m_iTextHeight = m_iRealLine * m_iFontHeight + 2 // canvas.setViewport(m_iTextWidth, m_iTextWidth) for (int i = 0, j = 0 i m_iTextWidth) { line++ istart = i i-- w = 0 } else { if (i == (string.length() - 1)) { line++ } } } } /**This is the requirement, asking for two lines*/ if (line > 2){ line = 2 } m_iTextHeight = (line) * m_iFontHeight + 2 } private int measureWidth(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec) int specSize = MeasureSpec.getSize(measureSpec) // Default size if no limits are specified. int result = 500 if (specMode == MeasureSpec.AT_MOST) { // Calculate the ideal size of your control // within this maximum size. // If your control fills the available space // return the outer bound. result = specSize } else if (specMode == MeasureSpec.EXACTLY) { // If your control can fit within these bounds return that value. result = specSize } return result } Public void SetText(String text) {// Note: This function can only draw text in the UI thread, in other threads

// //Can't draw text, find a long time to find the reason, ask the master to answer) string = text requestLayout() invalidate() } } public class DensityUtil { /** * dip converted to px */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density return (int) (dpValue * scale + 0.5f) } /** * px converted to dip */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density return (int) (pxValue / scale + 0.5f) } }

|_+_|

これは、テキストの幅と幅を指定してonDrawメソッドを呼び出すことによって行われます。ただし、android:paddingLeft、android:maxLinesなどのこれらのプロパティ構成は有用ではなく、使用するのも便利ではありません。





転載:https://my.oschina.net/u/141132/blog/389131