Attribute [attribute] Resource of Application resource series

Source: Internet
Author: User
Tags getcolor xml attribute

Attribute Resource: it is part of the entire Android Application resource. In fact, it is a bunch of instructions on how to add attributes to custom views on the Internet.ArticleThe attrs file in./RES/values/directory.
When someone else creates custom components you have developed through XML file configuration and dynamically sets attributes of your custom components, at this time, you need to add an XML Attribute resource file to your custom components to complete this task.
In fact, you can also complete the above functions without having to upload an XML Attribute resource file. In this way, your custom components seem simpler. A custom component is a class file, do not drag water. However, there are differences between the two methods, depending on your needs. The following are two implementation methods:

 

1: how to match the XML Attribute resource file


Step 1: attrs. xml file
First of all, you need to write the class file of the custom component, and then define the attribute that requires external input values in this class as an attribute resource file.
In the project .. create an attrs under the/RES/values/directory. the file name of an XML file is not only written in this way, so there is only one purpose for writing this file. At first glance, others will know that this file is an attribute resource file. The specific syntax is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <ATTR name = "test1" format = "string"/> <declare-styleable name = "myview"> <ATTR name = "textcolor" format = "color "/> <ATTR name = "textsize" format = "dimension"/> <ATTR name = "text" format = "string"/> </declare-styleable> </resources>
 
 

The description of attrs. XML is as follows:

 ATTR sub-element: defines a specific attribute. Format indicates the type of the value of this attribute, the following types are available: 1. reference: refer to the resource ID in the specified theme. This type means that the value you pass can be a reference resource 2. string: String. If you want others to directly write a value or use a method similar to "@ string/test" to reference a resource, you can write it as format = "string | reference" 3. color: Color 4. boolean: Boolean value 5. dimension: size value 6. float: Float 7. integer: integer 8. fraction: Percentage 9. enum: enumeration. If you provide attributes that can only be selected by others, 
   
    
    
   10. flag: bit or operation declare-styleable sub-element: defines a styleable object. Each styleable object is a set of ATTR attributes. Note: the name attribute here is not necessarily the same as the custom class name, just to distinguish the attributes of the corresponding class 
Note: after the property resource file defines this property, what is the role of this property in terms of which custom view component is used, this attribute resource file is not managed by this attribute resource file. That is to say, this attribute resource file is public and can be used by everyone. However, to facilitate management, generally, all attributes in a custom view are written as a declare-styleable set. what can be returned for a property defined by a property resource depends on the function of the custom componentCodeImplementation
 
 

Step 2: reference the attributes defined in the attrs file in the Custom class to set their own attributes.

 

Package cn.com. androidtest. ui; import cn.com. androidtest. r; import android. content. context; import android. content. res. typedarray; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rect; import android. util. attributeset; import android. view. view; public class myview extends view {private paint mpaint; private context mcontext; Private Static string mstring; private string test; Public myview (context) {super (context ); mpaint = new paint ();} public myview (context, attributeset attrs) {super (context, attrs); mpaint = new paint (); /* obtain the declare-styleable Set */typedarray typearray = context. obtainstyledattributes (attrs, R. styleable. myview);/* the corresponding property value is retrieved from the set. The second parameter is the default value */INT textcolor = typearray if the property is not configured. getcolor (R. styleable. myview_textcolor, 0 xffffffff); float textsize = typearray. getdimension (R. styleable. myview_textsize, 36); mstring = typearray. getstring (R. styleable. myview_text);/* set your own class member variable */mpaint. settextsize (textsize); mpaint. setcolor (textcolor);/* close Resources */typearray. recycle () ;}@ override protected void ondraw (canvas) {super. ondraw (canvas); mpaint. setstyle (style. fill); canvas. drawrect (New rect (10, 10, 90, 90), mpaint); mpaint. setcolor (color. blue); canvas. drawtext (mstring, 10,110, mpaint );}}

Step 3: Use custom components and set attributes

 

 
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: myandroid = "http://schemas.android.com/apk/res/cn.com.androidtest"
 Android: Orientation =" vertical "Android: layout_width =" fill_parent "Android: layout_height = "fill_parent"> 
   
    

Note: In Java code, the way to get attribute values, you must set a namespace for the custom component when using this component in XML [xmlns: myandroid = "http://schemas.android.com/apk/res/cn.com.androidtest"], otherwise, component properties cannot be set.
Namespace Syntax: xmlns: space name = "http://schemas.android.com/apk/res/self-defined kit package name"
Note the following when writing the package name:
If the package of your custom view is similar to the following two figures, the package name can only be written as the top-level package [cn.com. androidtest], not [cn.com. androidtest. UI]

Step 4: Finally, let's take a look.

2: XML resource files are not required.
This method is basically the same as the first method. It is only the part of the Java code that obtains the attribute value. It is a little different from the custom view used by others.
Java code writing:

/* Resource ID reference */INT resouceid =-1;/* obtain the resource ID. The first parameter is the namespace name. the second parameter is the property name set in the XML file. third parameter: Default Value */resouceid = attrs. getattributeresourcevalue (null, "textcolor", 0); If (resouceid> 0) textcolor = context. getresources (). getcolor (resouceid); resouceid = attrs. getattributeresourcevalue (null, "textcolor", 0); If (resouceid> 0) mstring = context. getresources (). gettext (resouceid, "http://wujiandong.iteye.com "). tostring ();

When used in XML, the namespace can be omitted, and the attribute name must be properly matched. OtherwiseProgramCannot be obtained. Unlike the XML resource file combination method, there is a constraint. Absolute Freedom equals no freedom ~~

 

 
   

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.