Android 自定义控件-TextView

很多时候系统自带的View满足不了设计的要求,就需要自定义View控件。自定义View首先要实现一个继承自View的类。添加类的构造方法,override父类的方法,如onDraw,(onMeasure)等。如果自定义的View有自己的属性,需要在values下建立attrs.xml文件,在其中定义属性,同时代码也要做修改。

一个简单的例子:

·新建一个MyView类,继承自TextView,并添加构造方法:

 

 

 

package com.example.custview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyView extends TextView {

    Paint mPaint = null;

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        mPaint = new Paint();
        TypedArray array = ctx
                .obtainStyledAttributes(attrs, R.styleable.MyView);

        int textColor = array
                .getColor(R.styleable.MyView_textColor, 0XFF00FF00); // 提供默认值,放置未指定
        float textSize = array.getDimension(R.styleable.MyView_textSize, 36);
        mPaint.setColor(textColor);
        mPaint.setTextSize(textSize);
        array.recycle();

    }

    @Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.draw(canvas);

        if (mPaint == null) {
            mPaint = new Paint();
        }

        mPaint.setColor(Color.RED);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);
        canvas.drawRect(0, 0, 20, 20, mPaint);
    }

}

再在主布局中使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:my="http://schemas.android.com/tools/res/com.example.custview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

<com.example.custview.MyView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    my:textColor="#FFFFFFFF"
    my:textSize="33dp" />
 
</RelativeLayout>

 

在图形布局中可以看到下面的结果:

 

即可完成。运行结果

 注意,因为在上面我们用到了布局,也就是自定义的控件是在xml配置的,所以一定要添加构造方法,(如果没有在布局中进行空间设置,这个构造方法可以不需要):

public MyView(Context context,AttributeSet attrs){

       super(context, attrs);  

    }



至少在xml文件中写上上面的内容。其中

com.example.custview.MyView这句是需要显示的控件所代表的类。这个类肯定是继承自View的自定义类(其实就是,使我们自己写的,这是废话了,可以是在工程中直接源码添加xxxx.java的,也可以是在libs目录下自己新添加的jar包里面的。如果是jar包里面的一个类,则路径就是jar包里面,这个类的路径。
完成上面的两步之后就可以在代码中实例化这个布局文件了

@Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //setContentView(new MyView(this));

显示的效果同上图。

 

下面介绍如何实现自定义View的属性设置。实现自定义View的属性设置,需要:

·在values目录下建立attrs.xml文件,添加属性内容

·在布局文件中添加新的命名空间xmlns,然后可以使用命名空间给自定义的空间设置属性

·设置完属性之后,当然还要对其进行处理。在自定义View类中的构造方法中进行处理

根据这三步给一个例子进行说明一下

首先添加attrs.xml文件,在定义属性

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

<resources> 

    <declare-styleable name="MyView"> 

    <attr name="textColor" format="color"/> 

    <attr name="textSize" format="dimension"/> 

    </declare-styleable> 

</resources>



public MyView(Context context,AttributeSet attrs){

       super(context, attrs);

         mPaint = new Paint();  

        //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组  

        //在使用完成后,一定要调用recycle方法  

        //属性的名称是styleable中的名称+“_”+属性名称  

        //TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);

        int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定  

        float textSize = array.getDimension(R.styleable.MyView_textSize, 36);  

        mPaint.setColor(textColor);  

        mPaint.setTextSize(textSize);  

        array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响  

      

    }

 
原文地址:https://www.cnblogs.com/zuiyirenjian/p/4164140.html