android自定义TextView(一)


自定义TextView,  实现基础的更改字符串中颜色..

通常实现更改,有的人想到使用多个textview 来实现,现在来自定义view, 来动态更改字符串颜色。

首先看下自定义view,

MuiltiColorTextView.java

public class MuiltiColorTextView extends TextView {
    private int mStart;
    private int mEnd;
    private int mTextColor;
    private SpannableStringBuilder mStyle;

    public MuiltiColorTextView(Context context) {
        this(context, null);
    }

    public MuiltiColorTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MuiltiColorTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context,attrs);
    }

    public void initView(Context context,AttributeSet attrs){
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.MultiTextColor);
        mStart = tArray.getInteger(R.styleable.MultiTextColor_text_start, 0);
        mEnd = tArray.getInteger(R.styleable.MultiTextColor_text_end, 0);
        mTextColor = tArray.getInteger(R.styleable.MultiTextColor_text_color, 0);
        tArray.recycle();
    }

    public void updateText() {
        if (this.getText().toString() != null){
            //将str字符串载入SpannableStringBuilder对象中
            mStyle = new SpannableStringBuilder(this.getText().toString());
        }
        if(mEnd<=getText().length()){
            mStyle.setSpan(new ForegroundColorSpan(mTextColor), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }
        setText(mStyle);
    }

    public void setmTextColor(int mTextColor){
        this.mTextColor=mTextColor;
    }
}

首先看看自定义view,继承TextView,   自然继承了 getText() ,,getTextSize() 等textView 的原始方法。

其中定义的变量  mStart  是字符串初始更改位置,mEnd是字符串结束更改位置,mTextColor是字符串更改颜色,SpannableStringBuilder是字符串拼接对象

 public void initView(Context context,AttributeSet attrs){
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.MultiTextColor);
        mStart = tArray.getInteger(R.styleable.MultiTextColor_text_start, 0);
        mEnd = tArray.getInteger(R.styleable.MultiTextColor_text_end, 0);
        mTextColor = tArray.getInteger(R.styleable.MultiTextColor_text_color, 0);
        tArray.recycle();
    }
这是自定义view ,获取自定义属性。

定义attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MultiTextColor">
        <attr name="text_start" format="integer"></attr>
        <attr name="text_end" format="integer"></attr>
        <attr name="text_color" format="integer"></attr>
    </declare-styleable>
</resources>

在activity中调用

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.getInstance().i("oncreate");
        setContentView(R.layout.activity_main);
        MuiltiColorTextView textView=(MuiltiColorTextView)findViewById(R.id.tv_view);
        textView.setmTextColor(getResources().getColor(R.color.colorAccent));
        textView.updateText();
    }
}

利用 自定义view,中的 setmTextColor() 设置更改颜色

updateText()做出更改字符串样式。


源码地址:

http://download.csdn.net/detail/xiabing082/9737424


参考

http://blog.csdn.net/lovexjyong/article/details/17021235






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值