您的位置:首页 > 移动开发 > Android开发

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

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

其一般步骤为:

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

需要注意的地方:

一.View的三个构造函数

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: