Use of Custom Attributes in Android

Source: Internet
Author: User

Android layout is a pleasure, thanks to its good XML method. You can use XML to quickly and effectively define the software interface. However, sometimes we always feel that some basic components officially defined are not enough, so custom components are inevitable. So how can we define its attributes in XML like those officially provided? Now let's discuss his usage.

1. Define an attrs. xml file under the Res/values file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "toolbar">
<ATTR name = "buttonnum" format = "integer"/>
<ATTR name = "itembackground" format = "reference | color"/>
</Declare-styleable>
</Resources>

2. Use this attribute in layout XML as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: toolbar = "http://schemas.android.com/apk/res/cn.zzm.toolbar"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<CN. zzm. toolbar. toolbar Android: Id = "@ + ID/gridview_toolbar"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignparentbottom = "true"
Android: Background = "@ drawable/control_bar"
Android: gravity = "center"
Toolbar: buttonnum = "5"
Toolbar: itembackground = "@ drawable/control_bar_item_bg"/>
</Relativelayout>

3. In custom components, you can get the values defined in XML as follows:

Typedarray A = context. obtainstyledattributes (attrs, R. styleable. toolbar );
Buttonnum = A. getint (R. styleable. toolbar_buttonnum, 5 );
Itembg = A. getresourceid (R. styleable. toolbar_itembackground,-1 );

A. Recycle ();

In this simple three-step process, you can use custom attributes.

**************************************** *****************************

Now, the basic usage has been completed. Let's take a look at some points of attention and knowledge.

First, let's take a look at the attrs. xml file.

This file defines the attribute name and format. You must use <declare-styleable name = "toolbar"> </declare-styleable> to enclose all attributes. Name indicates the name of the property set. It is used to identify the property set. Where will it be used? This is mainly in step 3. See? When obtaining an attribute identifier, "R. styleable. toolbar_buttonnum" is used. Obviously, "toolbar _" is added before each attribute _".

Let's take a look at the types of various attributes: String, integer, dimension, reference, color, and enum.

The preceding methods are consistent, for example, <ATTR name = "buttonnum" format = "integer"/>.
Only Enum is different. For example:

<ATTR name = "testenum">
<Enum name = "fill_parent" value = "-1"/>
<Enum name = "wrap_content" value = "-2"/>
</ATTR>

If two different attributes can be uploaded at the same time, you can split them with "|.

 

Let's take a look at the precautions in the layout XML.

First declare: xmlns: toolbar = http://schemas.android.com/apk/res/cn.zzm.toolbar
Note: The "toolbar" can be changed to any other name. The URL address must be the last part of the package name of the custom component. After customizing attributes, add "toolbar" before the attribute name.

 

Finally, let's take a look at the precautions in Java code.

In the constructor of a custom component, use

Typedarray A = context. obtainstyledattributes (attrs, R. styleable. toolbar );

To obtain the reference to the attribute set, and then you can use the "A" method to obtain the corresponding attribute value. Note that if the method used and the type of the obtained value are incorrect, the default value is returned. Therefore, if an attribute contains two or more non-essential attributes, you must make multiple judgments to determine the value to be assigned after reading the attribute. Of course, don't forget to recycle resources when you get the value!

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.