Android带括号多颜色TextView,TextView分段设置文本颜色(TextView左右文本不同颜色),Android动态设置TextView分段颜色ForegroundColorSpan

先看效果(下面一共就三个TextView哦)

布局代码部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.ycTest.MainActivity"
        android:fitsSystemWindows="true"
>

    <com.example.ycTest.YCCustomTextView
            android:id="@+id/customTV1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:padding="10dp"
            android:textSize="20dp"
            app:left_text="左侧文字"
            app:around_color="@color/drak999"
            android:text="中间文字"
            android:textColor="@color/theme1"
            app:right_text="右侧文字"
            app:left_color="@color/drak999"
            app:right_color="@color/girl_color"
    />

    <com.example.ycTest.YCCustomTextView
            android:id="@+id/customTV2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20dp"
            app:left_text="左侧文字"
            app:around_color="@color/drak999"
            android:text="中间文字"
            android:textColor="@color/theme1"
            app:right_text="右侧文字"
            app:have_bracket="true"
    />

    <com.example.ycTest.YCCustomTextView
            android:id="@+id/customTV3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:padding="10dp"
            android:textSize="20dp"
            app:left_text="左侧文字"
            android:text="中间文字"
            android:textColor="@color/theme1"
            app:right_text="右侧文字"
            app:left_color="@color/openService"
            app:right_color="@color/colorAccent"
    />

</LinearLayout>

带括号的使用方法

  <com.example.ycTest.YCCustomTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:padding="10dp"
            android:textSize="20dp"
            app:left_text="左侧文字"
            android:text="10"
            android:textColor="@color/theme1"
            app:left_color="@color/openService"
            app:have_bracket="true"       -----------是否含有括号默认为false 括号的颜色为textColor的颜色  
    />

 

动态设置部分

customTV1.setLeftText("123");//设置左侧文本
customTV1.setRightText("321");//设置右侧文本
customTV1.setText("   454   ");//中间文本(切记,setText一定要放在最后,否则数据会重复,算是个bug吧,暂时没时间细细调了)

 

使用是不是很简单,如果吸引到你了,你可以往下看

正式创建自定义View


public class YCCustomTextView extends TextView{
    private String leftText="";
    private String rightText="";
    private int color;
    private int left_color;
    private int right_color;
    private boolean have_bracket=false;
    public YCCustomTextView(Context context) {
        super(context);
        initParams(context,null);
    }

    public YCCustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initParams(context,attrs);
    }

    public YCCustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParams(context,attrs);
    }

    @Override
    public void setText(CharSequence text, TextView.BufferType type) {
        leftText= isNull(leftText)?"":leftText;
        rightText= isNull(rightText)?"":rightText;
        if(have_bracket) {
            text = leftText+"(" + text +")"+rightText;
        }else
            text = leftText + text + rightText;
        SpannableString spannableString = new SpannableString(text);
        if(left_color!= Color.BLACK) {
            if(!isNull(leftText))
                spannableString.setSpan(new ForegroundColorSpan(left_color), 0, leftText.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if(!isNull(rightText))
                spannableString.setSpan(new ForegroundColorSpan(right_color), text.length()-rightText.length()
                        , text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            super.setText(spannableString, type);
        } else if(color!= Color.BLACK) {
            if(!isNull(leftText))
                spannableString.setSpan(new ForegroundColorSpan(color), 0, leftText.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if(!isNull(rightText))
                spannableString.setSpan(new ForegroundColorSpan(color), text.length()-rightText.length()
                        , text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                super.setText(spannableString, type);
        }else
            super.setText(text, type);
    }
    public void setLeftText(String text){
        leftText=text;
        setText(getText());
    }
    public void setRightText(String text){
        rightText=text;
        setText(getText());
    }

    public void initParams(Context context,AttributeSet attributeSet){
        TypedArray typedArray=context.obtainStyledAttributes(attributeSet, R.styleable.YCCustomTextView);//注意styleable 即attr 的命名要与自定义控件的名字一样 否则无法代码不会自动补齐,也不会有代码提示,只能挨个字母敲完
        if(typedArray!=null){
            leftText=typedArray.getString(R.styleable.YCCustomTextView_left_text);
            rightText=typedArray.getString(R.styleable.YCCustomTextView_right_text);
            color = typedArray.getColor(R.styleable.YCCustomTextView_around_color, Color.BLACK);
            left_color = typedArray.getColor(R.styleable.YCCustomTextView_left_color, Color.BLACK);
            right_color= typedArray.getColor(R.styleable.YCCustomTextView_right_color, Color.BLACK);
            have_bracket= typedArray.getBoolean(R.styleable.YCCustomTextView_have_bracket, false);
        }
        setText(getText());//重置一下控件
    }

    /**
     * 判断是否为空  若存在就返回
     */
    public static boolean isNull(String... str) {
        for (int i = 0; i < str.length; i++) {
            if (str[i] == null)
                return true;
            if (str[i].length() == 0)
                return true;
            if(str[i].equals("null"))
                return true;
        }
        return false;
    }
}

在values文件夹下创建attr.xml

在里面添加declare-styleable

 <declare-styleable name="YCCustomTextView">
        <attr name="left_text" format="string"/><!--   左侧字体   -->
        <attr name="right_text" format="string"/><!--   右侧字体  -->
        <attr name="around_color" format="color"/><!--  环绕字体颜色   -->
        <attr name="have_bracket" format="boolean"/><!--  是否有括号   -->
        <attr name="left_color" format="color"/><!--  左侧字体颜色    -->
        <attr name="right_color" format="color"/><!--   右侧字体颜色    -->
    </declare-styleable>

如果不设置around_color或者left_color或者right_color所有字体显示的颜色为textColor的颜色

源码地址:https://download.csdn.net/download/chxc_yy/11858493

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值