首页 > 移动平台 > 详细

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

时间:2014-04-18 10:13:28      阅读:658      评论:0      收藏:0      [点我收藏+]

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

 

其一般步骤为:

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


 

需要注意的地方:

一.View的三个构造函数
bubuko.com,布布扣

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

 

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

 

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

 


二.View重要的回调

bubuko.com,布布扣

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

 

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

 

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

 

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

 


三.设置自定义属性

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

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

定义属性组

在构造函数中获取这些值


 

 

Example:

1.myButton.java

 

bubuko.com,布布扣
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+"");
        }
    }
}
myButton.java

 

2.attrs.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="myButton">
        <attr name="step" format="integer"/>
    </declare-styleable>
</resources>
attrs.xml

 

3.使用自定义控件

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

bubuko.com,布布扣
<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>
MainActivity

 

 

[Android学习笔记]自定义控件的使用,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/hellenism/p/3672351.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!