[Android] Create a view class

Source: Internet
Author: User

Like any other well-designed class, our own definition of the view class encapsulates a series of functional interfaces, effectively using CPU and memory, and so on. To be a well-designed class, our view class also needs to have:

    • comply with Android standards;
    • Provides custom style attributes through the Android XML layout;
    • Send up to the event;
    • Compatible with a wide range of Android platforms.

The Android framework provides a range of basic types and XML tags to help developers create view classes that meet these requirements. This is detailed below.

1. Sub-Class View class:

All view classes in Android are derived from the view class. Our view classes can be derived directly from the view class or from a high-level class, such as the Button class. In order for Android Developer tools to interact with our view class, we must provide at least one constructor with the context and AttributeSet objects as arguments. Layout Editor uses this constructor to create and edit our view class instances.

2. Define our own properties:

In order to add the built-in view to our interface, we can use it in XML (such as layout) and control their appearance and behavior through the built-in element properties. Our own view classes can also be used in this way, even with our own properties, as long as we:

    • Define our own attributes in the <declare-styleable> element;
    • Specify attribute values in XML layout;
    • Gets or sets the property value at run time;
    • Apply the Acquired property value to our view.

How do we define our own properties? In general, it is defined in a res/values/attrs.xml file, and of course it seems to be a simple resource, so it can appear in other files.

Location of the file:
res/values/attrs.xml
The file name can be optional. The element name is the resource ID name.
References to resources:
Xml: @[namespace:]attribute
Syntax
Elements:
<resources>
Required element, and must be the root element. No attributes.
<declare-styleable>
Define the wash of a styleable series. Contains <attr> elements.
Property:
name
string, required, style name.
<item>
Define a single attribute for the style, which must be a child of the <style> element.
Properties:
name
property Resource, required, the name of the style attribute to be defined (typically a system attribute, such as: android:textColor ).

We'll show you how to define our own properties and how to specify property values.

This sections 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 a attrs.xml file:

[Android] Create a view class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.