当前位置:   article > 正文

自定义控件解决android中TextView中英文换行问题_android textview 英文单词在第一行末尾展示不下单词分开展示,不换行

android textview 英文单词在第一行末尾展示不下单词分开展示,不换行

现有textView存在的问题

 <TextView
        android:id="@+id/id_normal"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>
  • 1
  • 2
  • 3
  • 4
  • 5

设置如下文字效果图

这里写图片描述

这里写图片描述

从上面的设置中可以明显看出,当文本中即含有中文,又含有英文的时候,文本并不是在每行的末尾换行的,这样就造成了我们的UI界面难看;

解决方法

这里采用自定义View的方式写了一个可以适配文本的控件

使用方法
  • maxlines表示需要显示的最大行数,通过该值生成对应的子textView并添加到布局中
  • 通过测量的方式paint.breakText(mText, start, mText.length(), true, mWidth, null);计算出每一行所要设置的文本长度
<com.qianxi.view.NameTextView
    android:id="@+id/id_name"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:background="#ff0"
    android:maxLines="2"
    android:padding="5dp"
    android:textColor="#f00"
    android:textSize="16sp">

</com.qianxi.view.NameTextView>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
完整代码
public class NameTextView extends LinearLayout {
    private final String NAME_SPACE = "http://schemas.android.com/apk/res/android";
    private final String ATTR_TEXT_COLOR = "textColor";
    private final String ATTR_TEXT_SIZE = "textSize";
    private final String ATTR_MAX_LINES = "maxLines";

    private final int DEFAULT_COLOR = 0x88000000;

    private Context mContext;
    private int mTextColor, mTextSize, mMaxLines;

    private String mText = "";// 要写入的文本

    private int mWidth;// 控件宽度

    private List<TextView> mChildViews;

    private boolean canDraw = false;

    public NameTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        if (attrs != null) {
            mMaxLines = attrs.getAttributeIntValue(NAME_SPACE, ATTR_MAX_LINES, 2);
            mTextColor = attrs.getAttributeIntValue(NAME_SPACE, ATTR_TEXT_COLOR, DEFAULT_COLOR);
            String size = attrs.getAttributeValue(NAME_SPACE, ATTR_TEXT_SIZE);
            if (TextUtils.isEmpty(size)) {
                mTextSize = 12;
            } else {
                mTextSize = Integer.parseInt(size.substring(0, size.lastIndexOf(".")));
            }
        }

        initSelf();
        initView();
    }

    private void initSelf() {
        setOrientation(VERTICAL);
        setWillNotDraw(true);
    }

    private void initView() {
        mChildViews = new ArrayList<>();
        for (int i = 0; i < mMaxLines; i++) {
            TextView v = createChildTextView();
            mChildViews.add(v);
            addView(v);
        }
    }

    /**
     * 构建子的TextView
     */
    private TextView createChildTextView() {
        TextView v = new TextView(mContext);
        v.setTextColor(mTextColor);
        v.setTextSize(mTextSize);
        v.setMaxLines(1);
        v.setSingleLine();
        v.setEllipsize(TruncateAt.END);
        v.setGravity(Gravity.CENTER_VERTICAL);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        v.setLayoutParams(params);

        return v;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (canDraw) {
            setContent();
            canDraw = false;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mWidth = w - getPaddingLeft() - getPaddingRight();
    }

    /**
     * set the content
     *
     * @param text content
     */
    @SuppressLint("NewApi")
    public void setText(@NonNull String text) {
        if (TextUtils.isEmpty(text) || this.mText.equals(text)) return;
        this.mText = text.trim();
        canDraw = true;
        invalidate();
    }

    /**
     * @param text content
     * @param line content-line
     */
    @SuppressLint("NewApi")
    public void setText(@NonNull String text, int line) {
        if (TextUtils.isEmpty(text) || this.mText.equals(text)) return;
        this.mMaxLines = line;
        this.mText = text.trim();
        canDraw = true;
        invalidate();
    }

    /**
     * 将内容设置到布局中
     */
    private void setContent() {
        Paint paint = mChildViews.get(0).getPaint();
        int start = 0;

        for (int i = 0; i < mMaxLines; i++) {
            TextView v = mChildViews.get(i);

            String str;
            if (i == mMaxLines - 1) {
                str = mText.substring(start);
            } else {
                int count = paint.breakText(mText, start, mText.length(), true, mWidth, null);
                str = mText.substring(start, start + count);
                start += count;
            }
            Log.d("NameTextView", "i=" + i + "=====" + str);
            v.setText(str);
            if (start >= mText.length()) break;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/242931
推荐阅读
相关标签
  

闽ICP备14008679号