有时候,需要对 TextView 中的文字进行一些操控,主要用到 SpannableString

1、部分颜色设置
2、部分字体大小设置
3、图片设置
4、部分字体背景设置
5、部分文字下划线点击设置
6、所有文字下划线设置
7、所有文字中划线设置
8、所有文字中划线设置(加清晰)
9、文字加粗
10、段落首行缩进
11、整体作用

 

12、补充

TextView占据的行数

android:lines="2"
android:ellipsize="end"

如果想动态占行,可以用

android:maxLines="2"

指定最大行数

设置多出部分用省略号展示

android:singleLine="true"

 

13、文字加粗(代码加粗)

1、将中文设置成粗体的方法是 setFakeBoldText 设置为true:
TextView textView = findViewById(R.id.TextView01);  
TextPaint textPaint = tv.getPaint();
textPaint.setFakeBoldText(true); 
2、要取消加粗效果的话设置为false:
TextPaint textPaint = textView.getPaint();
textPaint.setFakeBoldText(false); 

实现效果

android textview字体设置大小 安卓textview字体加粗_Android

实现代码
activity_text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical"
    tools:context=".ui.TextViewActivity">

    <TextView
        android:id="@+id/tv_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tv_color" />

    <TextView
        android:id="@+id/tv_big"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_big"/>

    <TextView
        android:id="@+id/tv_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/tv_image"/>

    <TextView
        android:id="@+id/tv_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_background"/>

    <TextView
        android:id="@+id/tv_line"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_line"/>

    <TextView
        android:id="@+id/tv_line_bottom_all"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_line_bottom_all"/>

    <TextView
        android:id="@+id/tv_in_line_all"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_in_line_all"/>

    <TextView
        android:id="@+id/tv_in_line_all_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_in_line_all_clear"/>

    <TextView
        android:id="@+id/tv_hickening"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_hickening"/>

    <TextView
        android:id="@+id/tv_paragraph"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/tv_line"
        android:layout_marginTop="20dp"
        android:text="@string/tv_paragraph"/>

    <TextView
        android:id="@+id/tv_aplication"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/tv_aplication"/>

</LinearLayout>

strings.xml

<string name="tv_color">色光三原色为:红、绿、蓝,美术三原色为:红、黄、青</string>
<string name="tv_big">听说炫酷的TextView能放大</string>
<string name="tv_image">在里面加图片(图)也行</string>
<string name="tv_background">也能够改变局部背景</string>
<string name="tv_line">还能够划重点</string>
<string name="tv_line_bottom_all">所有文字加下滑线</string>
<string name="tv_in_line_all">所有文字加中滑线</string>
<string name="tv_in_line_all_clear">所有文字加中滑线(加清晰)</string>
<string name="tv_hickening">文字加粗</string>t
<string name="tv_paragraph">段落的首行缩进效果</string>
<string name="tv_aplication">家庭组是占用硬盘的最大原因之一,因此我们需要做的就是将家庭组的服务项关闭。操作方法:右键点击此电脑-管理-服务和应用程序-服务,找到HomeGroup Listener和HomeGroup Provider双击后选择禁用。</string>

 

TextViewActivity.java

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.self.control.R;
import com.self.control.base.BaseActivity;

import butterknife.BindView;

public class TextViewActivity extends BaseActivity {

    @BindView(R.id.tv_color)
    TextView tvColor;
    @BindView(R.id.tv_big)
    TextView tvBig;
    @BindView(R.id.tv_image)
    TextView tvImage;
    @BindView(R.id.tv_background)
    TextView tvBackground;
    @BindView(R.id.tv_line)
    TextView tvLine;
    @BindView(R.id.tv_paragraph)
    TextView tvParagraph;
    @BindView(R.id.tv_aplication)
    TextView tvAplication;
    @BindView(R.id.tv_line_bottom_all)
    TextView tvLineBottomAll;
    @BindView(R.id.tv_in_line_all)
    TextView tvInLineAll;
    @BindView(R.id.tv_in_line_all_clear)
    TextView tvInLineAllClear;
    @BindView(R.id.tv_hickening)
    TextView tvHickening;

    private SpannableStringBuilder span;
    private SpannableString spannableString;

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_text_view;
    }

    @SuppressLint("NewApi")
    @Override
    public void initView(View view) {
        //部分字体颜色
        spannableString = new SpannableString(tvColor.getText().toString());
        spannableString.setSpan(new ForegroundColorSpan(Color.RED), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 9, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.RED), 20, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.YELLOW), 22, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.young)), 24, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvColor.setText(spannableString);

        //部分字体放大
        spannableString = new SpannableString(tvBig.getText().toString());
        spannableString.setSpan(new AbsoluteSizeSpan(50), 14, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvBig.setText(spannableString);

        //添加图片
        /**
         * 通过setCompoundDrawablesRelativeWithIntrinsicBounds 方法可以向 TextView 的上下左右添加图片,有两种添加方法:(默认居中)
         * setCompoundDrawablesRelativeWithIntrinsicBounds(@DrawableRes int start, @DrawableRes int top, @DrawableRes int end, @DrawableRes int bottom)
         * setCompoundDrawablesRelativeWithIntrinsicBounds(@Nullable Drawable start,@Nullable Drawable top, @Nullable Drawable end, @Nullable Drawable bottom)
         */
        tvImage.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.mipmap.ic_launcher_round,
                R.mipmap.ic_launcher_round,//getResources().getDrawable(R.mipmap.ic_launcher_round)
                R.mipmap.ic_launcher_round,
                R.mipmap.ic_launcher_round);
        //文字中间添加图片
        spannableString = new SpannableString(tvImage.getText().toString());
        spannableString.setSpan(new ImageSpan(this, R.mipmap.ic_launcher_round), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvImage.setText(spannableString);

        //局部布局背景
        spannableString = new SpannableString(tvBackground.getText().toString());
        spannableString.setSpan(new BackgroundColorSpan(Color.RED), 7, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvBackground.setText(spannableString);

        //划重点,删除划线
        spannableString = new SpannableString(tvLine.getText().toString());
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.RED);       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }

            @Override
            public void onClick(View widget) {
                //设置点击事件
                Toast.makeText(TextViewActivity.this, "重点", Toast.LENGTH_SHORT).show();
            }
        }, 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvLine.setText(spannableString);
        tvLine.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明,否则会一直出现高亮(背景)
        tvLine.setMovementMethod(LinkMovementMethod.getInstance());//设置响应点击事件

        //所有文字下划线
        tvLineBottomAll.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tvLineBottomAll.getPaint().setAntiAlias(true);//抗锯齿

        //所有文字中划线
        tvInLineAll.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        tvInLineAll.getPaint().setAntiAlias(true);

        //所有文字中划线(加清晰)
        tvInLineAllClear.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        //tvInLineAllClear.getPaint().setAntiAlias(true);

        //文字加粗
        tvHickening .getPaint().setFakeBoldText(true);

        //首行缩进(隐藏前面两个字)
        span = new SpannableStringBuilder("缩进" + tvParagraph.getText());
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvParagraph.setText(span);

        //整体应用
        span = new SpannableStringBuilder("缩进" + tvAplication.getText());
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvAplication.setText(span);
        spannableString = new SpannableString(tvAplication.getText().toString());
        spannableString.setSpan(new ForegroundColorSpan(Color.RED), 15, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(55), 22, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ImageSpan(this, R.mipmap.ic_launcher_round), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new BackgroundColorSpan(Color.RED), 27, 29, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //ds.setColor(Color.RED);       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }

            @Override
            public void onClick(View widget) {
                //设置点击事件
                Toast.makeText(TextViewActivity.this, "也是重点", Toast.LENGTH_SHORT).show();
            }
        }, tvAplication.length() - 5, tvAplication.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvAplication.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明,否则会一直出现高亮(背景)
        tvAplication.setMovementMethod(LinkMovementMethod.getInstance());//设置响应点击事件
        tvAplication.setText(spannableString);
        tvAplication.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.mipmap.ic_launcher_round,
                R.mipmap.ic_launcher_round,//getResources().getDrawable(R.mipmap.ic_launcher_round)
                R.mipmap.ic_launcher_round,
                R.mipmap.ic_launcher_round);
    }

    @Override
    public void doBusiness(Context mContext) {

    }
}

 

 

 

GitHub:https://github.com/iscopy/Control