[Android学习笔记]自定义控件的使用

From: http://www.cnblogs.com/hellenism/p/3672351.html

自定义控件时,最好抽象得彻底,并且编写需严谨,因为可能程序中多处都会引用到它,或者提供给团队中的其他人使用。

 

其一般步骤为:

1.创建控件的类文件,定义其功能逻辑。一般继承自现有控件或者View
2.在res/values目录下创建attrs.xml文件,用于定义该控件的xml标签属性,方便在使用xml声明该控件时设置参数
3.实现该控件的构造器,在构造器中把xml标签属性与后台代码中的变量相连接
4.完成以上步骤之后,便可使用该控件


 

需要注意的地方:

一.View的三个构造函数

第一个构造函数:
当不需要使用xml声明或者不需要使用inflate动态加载时候,实现此构造函数即可

 


/**
	 * Constructor that is called when inflating a view from XML. This is called
	 * when a view is being constructed from an XML file, supplying attributes
	 * that were specified in the XML file. This version uses a default style of
	 * 0, so the only attribute values applied are those in the Context's Theme
	 * and the given AttributeSet.
	 *
	 * <p>
	 * The method onFinishInflate() will be called after all children have been
	 * added.
	 *
	 * @param context The Context the view is running in, through which it can
	 *        access the current theme, resources, etc.
	 * @param attrs The attributes of the XML tag that is inflating the view.
	 * @see #View(Context, AttributeSet, int)
	 */
	public View(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}


第二个构造函数:
当需要在xml中声明此控件,则需要实现此构造函数。并且在构造函数中把自定义的属性与控件的数据成员连接起来。

 

第三个构造函数:
接受一个style资源

 


二.View重要的回调

onFinishInflate()
在此控件被通过xml声明的方式创建之后调用

 

onMeasure(in,int)
计算本控件的宽高,如果继承自原有控件,则一般不需要重写此方法

 

onLayout()
用于布局控件,对于不是继承ViewGroup的控件,一般不需要重写此方法

 

onDraw()
在绘制控件时候调用,控件具体长什么样子就在此方法中实现

 


 

 

这些方法的调用顺序是:

一个控件从创建到显示到屏幕:

onLayout  ->  onMeasure  ->  onLayout  ->  onDraw

之后,(Button为例)点击Button,都会调用onDraw,因为有按下效果,控件发生了重绘


 


三.设置自定义属性

使用自定义控件时候,需要通过把xml中声明的属性与控件的数据成员连接起来

在res/values目录下创建attrs.xml文件

定义属性组

在构造函数中获取这些值


 

 

Example:

1.myButton.java

 

复制代码
public class myButton extends Button
{
    private int step;
    
    public myButton(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        init(attrs);
    }

    public myButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init(attrs);
    }

    public myButton(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    private void init(AttributeSet attrs)
    {
        if (attrs != null)
        {
            TypedArray a = getContext().obtainStyledAttributes(attrs,
                    R.styleable.myButton);
            
             step = a.getInt(R.styleable.myButton_step, 0);
             
             a.recycle();
             
             this.setText(step+"");
        }
    }
}
复制代码

 

2.attrs.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="myButton">
        <attr name="step" format="integer"/>
    </declare-styleable>
</resources>
复制代码

 

3.使用自定义控件

xmlns:views="http://schemas.android.com/apk/res/" + 子包名

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" 
    
    xmlns:views="http://schemas.android.com/apk/res/com.example.createvewtest">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <com.example.views.myButton 
        android:id="@+id/btn"
        android:text="button"
        views:step="10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
复制代码

 

 

人生就是一局不能Again的DOTA

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值