如何根据布局的宽度显示字符串

Shr*_*a S 8 android textview android-layout android-linearlayout

我创建了两个垂直宽度相等的布局.我有字符串数据动态显示在文本视图上.当字符串大于布局的宽度时,字符串将被包装到布局的宽度,对于剩余的字符串,我想动态创建新的电视.此过程结束,直到剩余的字符串结束.对于下一个字符串相同的过程继 当进程到达linearlayout1的底部时,剩余的字符串应该从linearlayout2开始.并且该过程继续直到它到达linearlayout2的底部.

我试过这样的

private void nextlinechar(int numChars,String devstr) {
    nextchar=devstr;   
    Log.d("char 1",""+nextchar);
    TextView sub=new TextView(getApplicationContext());
    sub.setLines(1);
    sub.setTextColor(Color.BLACK);
    sub.setTextSize(textsize);
    sub.setText(nextchar); 
    nextchar=devstr.substring(nextcharstart);
    String textToBeSplit = nextchar; // Text you want to split between TextViews
    String data=TextMeasure(nextchar,sub);
    float myTextSize=sub.getTextSize();
    float textView2Width=400;

 // String next=TextMeasure(nextchar,sub);
    Paint paint = new Paint();    
    paint.setTextSize(myTextSize); // Your text size
    numChars1= paint.breakText(textToBeSplit, true,textView2Width, null);
    nextchar1=nextchar.substring(numChars1);
 // Log.d("char",""+i+"  "+nextchar.length());
    main.addView(sub);   
    nextlinechar(numChars1,nextchar);
}
Run Code Online (Sandbox Code Playgroud)

插图

在此输入图像描述

Sur*_*raj 0

参考这可能对您有帮助:

 package com.example.demo;

    import android.content.Context;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.widget.TextView;

    public class FontFitTextView extends TextView {

        public FontFitTextView(Context context) {
            super(context);
            initialise();
        }

        public FontFitTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initialise();
        }

        private void initialise() {
            mTestPaint = new Paint();
            mTestPaint.set(this.getPaint());
            // max size defaults to the initially specified text size unless it is
            // too small
        }

        /*
         * Re size the font so the specified text fits in the text box assuming the
         * text box is the specified width.
         */
        private void refitText(String text, int textWidth) {
            if (textWidth <= 0)
                return;
            int targetWidth = textWidth - this.getPaddingLeft()
                    - this.getPaddingRight();
            float hi = 100;
            float lo = 2;
            final float threshold = 0.5f; // How close we have to be

            mTestPaint.set(this.getPaint());

            while ((hi - lo) > threshold) {
                float size = (hi + lo) / 2;
                mTestPaint.setTextSize(size);
                if (mTestPaint.measureText(text) >= targetWidth)
                    hi = size; // too big
                else
                    lo = size; // too small
            }
            // Use lo so that we undershoot rather than overshoot
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            int height = getMeasuredHeight();
            refitText(this.getText().toString(), parentWidth);
            this.setMeasuredDimension(parentWidth, height);
        }

        @Override
        protected void onTextChanged(final CharSequence text, final int start,
                final int before, final int after) {
            refitText(text.toString(), this.getWidth());
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            if (w != oldw) {
                refitText(this.getText().toString(), w);
            }
        }

        // Attributes
        private Paint mTestPaint;
    }
Run Code Online (Sandbox Code Playgroud)