跟其他任何设计良好的类一样,我们自己定义的视图类也封装了系列功能接口,有效地使用CPU和内存等等。要成为一个设计优良的类,我们的视图类还需要具有:
安卓框架提供一系列基本类型和XML标签来帮助开发者创建满足以上要求的视图类。下面将详细介绍。
安卓中所有的视图类都派生于view类。我们的视图类可以直接派生于view类,也可以派生于高层类,譬如Button类。为了让Android Developer Tools能和我们的视图类交互,我们至少要提供一个带Context和AttributeSet对象作为参数的构造函数。layout editor使用这个构造函数来创建和编辑我们的视图类实例。
为了把内建视图加入到我们的界面之中,我们可以在XML中使用它(譬如layout)并通过内建元素属性控制它们的外观和行为。我们自己的视图类也可以这样使用,甚至还可以使用我们自己的属性,只要我们:
如何定义我们自己的属性呢?一般来说,是在res/values/attrs.xml文件中定义,当然看上去这也是一种简单资源,所以可以出现在其它文件中。
res/values/attrs.xml@[namespace:]attribute下面我们将介绍如何定义我们自己的属性以及如何指定属性值。
This section discusses how to define custom attributes and specify their values. The next section deals with retrieving and applying the values at runtime.
To define custom attributes, add <declare-styleable> resources to your project. It‘s customary to put these resources into a res/values/attrs.xml file. Here‘s an example of an attrs.xml file:
原文:http://www.cnblogs.com/victor-ma/p/4435602.html