[android自定义控件]之自定义TextView实现两个text

       android自定义控件对于每一个android程序员来说是基本技能,今天写了一个自定义TextView,通过xml定义属性leftText和rightText实现在一个view里面实现两个text并且设置相应属性。

1.在res/values/attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="leftText" format="string"></attr>
    <attr name="leftTextSize" format="dimension"></attr>
    <attr name="leftTextColor" format="color"></attr>
    <attr name="rightText" format="string"></attr>
    <attr name="rightTextSize" format="dimension"></attr>
    <attr name="rightTextColor" format="color"></attr>
    <declare-styleable name="customTextView">
        <attr name="leftText"/>
        <attr name="leftTextSize"/>
        <attr name="leftTextColor"/>
        <attr name="rightText"/>
        <attr name="rightTextSize"/>
        <attr name="rightTextColor"/>
    </declare-styleable>

</resources>

2.在构造方法中获得我们需要的属性

 public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.customTextView, defStyleAttr, 0);
        int num = a.getIndexCount();
        for (int i = 0;i<num;i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.customTextView_leftText:
                    leftText = a.getString(attr);
                    break;
                case R.styleable.customTextView_leftTextColor:
                    leftTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.customTextView_leftTextSize:
                    leftTextSize = a.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics()));
                    break;
                case R.styleable.customTextView_rightText:
                    rightText = a.getString(attr);
                    break;
                case R.styleable.customTextView_rightTextColor:
                    rightTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.customTextView_rightTextSize:
                    rightTextSize = a.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();

        mPaint = new Paint();
        mPaint.setTextSize(leftTextSize);
        mBound = new Rect();
        mPaint.getTextBounds(leftText, 0, leftText.length(), mBound);

        rightPiant = new Paint();
        rightPiant.setTextSize(rightTextSize);
        rightBuound = new Rect();
        rightPiant.getTextBounds(rightText,0,rightText.length(),rightBuound);
    }

3.重写onDraw

    @Override
    protected void onDraw(Canvas canvas) {

        mPaint.setColor(Color.BLACK);

        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        mPaint.setColor(leftTextColor);

        canvas.drawText(leftText, getWidth() /10 , getHeight() / 2 + mBound.height() / 2, mPaint);

        rightPiant.setColor(rightTextColor);
        canvas.drawText(rightText, getWidth() / 2 -rightBuound.width() , getHeight() / 2 + rightBuound.height() / 2, rightPiant);
        super.onDraw(canvas);
    }


下面在xml里面定义CustomTextView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  <cn.sundroid.customtextview.widget.CustomTextView
      android:layout_width="match_parent"
      android:layout_height="50dp"
      custom:leftText="爱生活"
      custom:leftTextColor="#ffffff"
      custom:leftTextSize="16sp"
      custom:rightText="爱android"
      custom:rightTextColor="#a4ff91"
      custom:rightTextSize="16sp"
 />

</RelativeLayout>

下面是实现的效果


好的,就这样吧!

源代码下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值