安卓文本设置

package com.junfeng.mytextview;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


     textView = findViewById(R.id.text);
    initAutoSplitTextView();
    String s2 = "abcdefghijklmnopqrs tuvwxyz12345678901234567891234";
    SpannableString spannableString2 = new SpannableString("中华人民共和国手机打开速度看见撒旦卡萨丁嘎斯的沙皇的韩国你还得似乎覅");
    SpannableString spannableString = new SpannableString(s2);

    Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);

// drawable.setBounds(0,50,50,20);
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
textView.setCompoundDrawablePadding(4);
ImageSpan imageSpan = new ImageSpan(drawable);
// spannableString.setSpan(drawable,spannableString.length()-1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,“点击了图片”,Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);//去除下划线
}
};
spannableString.setSpan(clickableSpan,spannableString.length()-1, spannableString.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
//需要设置这个才能点击
textView.setMovementMethod(LinkMovementMethod.getInstance());
//去掉高亮,不然点击时候背景会变高亮
textView.setHighlightColor(getResources().getColor(android.R.color.transparent));
}

private void initAutoSplitTextView() {
    textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            final CharSequence newText = autoSplitText(textView);
            if (!TextUtils.isEmpty(newText)) {
                textView.setText(newText);
            }
        }
    });
}

//返回CharSequence对象
private CharSequence autoSplitText(final TextView tv) {
    //tv.getText(), 原始的CharSequence内容.
    CharSequence charSequence = tv.getText();
    final String rawText = tv.getText().toString(); //原始文本
    final Paint tvPaint = tv.getPaint(); //paint,包含字体等信息
    final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度

    //将原始文本按行拆分
    String[] rawTextLines = rawText.replaceAll("\r", "").split("\n");
    StringBuilder newStringBuilder = new StringBuilder();
    for (String rawTextLine : rawTextLines) {
        if (tvPaint.measureText(rawTextLine) <= tvWidth) {
            //如果整行宽度在控件可用宽度之内,就不处理了
            newStringBuilder.append(rawTextLine);
        } else {
            //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行
            float lineWidth = 0;
            for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {
                char ch = rawTextLine.charAt(cnt);
                lineWidth += tvPaint.measureText(String.valueOf(ch));
                if (lineWidth <= tvWidth) {
                    newStringBuilder.append(ch);
                } else {
                    newStringBuilder.append("\n");
                    lineWidth = 0;
                    --cnt;
                }
            }
        }
        newStringBuilder.append("\n");
    }

    //把结尾多余的\n去掉
    if (!rawText.endsWith("\n")) {
        newStringBuilder.deleteCharAt(newStringBuilder.length() - 1);
    }

    //对于有富文本的情况
    if (charSequence instanceof Spanned) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(charSequence);
        if (newStringBuilder.toString().contains("\n")) {
            String[] split = newStringBuilder.toString().split("\n");
            int tempIndex = 0;
            for (int i = 0; i < split.length; i++) {
                if (i != split.length - 1) {
                    String s = split[i];
                    tempIndex = tempIndex + s.length() + i;
                    spannableStringBuilder.insert(tempIndex, "\n");
                }
            }
        }
        return spannableStringBuilder;
    } else {
        return newStringBuilder;
    }
}

}

<?xml version="1.0" encoding="utf-8"?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值