Android 自定义控件-TextView

简介: 很多时候系统自带的View满足不了设计的要求,就需要自定义View控件。自定义View首先要实现一个继承自View的类。添加类的构造方法,override父类的方法,如onDraw,(onMeasure)等。

很多时候系统自带的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(); //一定要调用,否则这次的设定会对下次的使用造成影响  

      

    }

 

 

相关文章
|
5月前
|
XML 前端开发 Java
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
41 1
|
29天前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
2月前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
12 0
|
5月前
|
XML Java Android开发
Android Studio App自定义控件中视图的构造和测量方法讲解及实战(附源码 实现下拉刷新功能 超详细必看)
Android Studio App自定义控件中视图的构造和测量方法讲解及实战(附源码 实现下拉刷新功能 超详细必看)
44 1
|
9月前
|
XML Android开发 数据格式
Android 中使用SpannableString实现TextView文本超链接跳转功能
Android 中使用SpannableString实现TextView文本超链接跳转功能
161 0
|
9月前
|
Android开发
Android 实现视图文本TextView的展开与收缩功能
Android 实现视图文本TextView的展开与收缩功能
214 0
|
9月前
|
Java Android开发 iOS开发
Android TextView 与 EditText 的区别与使用
Android TextView 与 EditText 的区别与使用
99 0
|
12月前
|
缓存 Android开发 Kotlin
Android 弹幕的两种实现及性能对比 | 自定义控件
Android 弹幕的两种实现及性能对比 | 自定义控件
201 0
|
12月前
|
调度 Android开发
Android空间架构与自定义控件详解-更新中
Android空间架构与自定义控件详解-更新中
62 0
|
12月前
|
Android开发
Android控件 TextView属性大全
Android控件 TextView属性大全