Properties of the Android app Resource family (Attribute) resource

Source: Internet
Author: User
Tags getcolor xml attribute

Transferred from: http://wujiandong.iteye.com/blog/1184921
    • Android
Anroidattribute Custom Component Properties Properties (Attribute) resource: is part of the entire Android app resource. In fact, it's a bunch of online descriptions of how to add your own properties to a custom view article in the Attrs file, which is located in the. Under the/res/values/directory

When someone creates a custom component that you have developed in the form of an XML file configuration and can also dynamically set the properties of your custom component, you need to give your own custom component an XML attribute resource file to complete the work.

In fact, you can not be unworthy of an XML property resource file, but also to complete the function as above, so that your custom components appear a little more straightforward, a custom component is a class file, not the muddy. But these two ways are different, specifically to see their own needs to choose, the following are two ways to achieve:


1: How to work with XML property resource files
First step: Attrs.xml file
The first thing to do is to write the class file that defines the component, and then define the attribute in the class that requires an external pass-in value as a property resource file.
In the works. /res/values/directory to create a attrs.xml file, the file name is not only written like this, so write only one purpose, others will know that this file is a property resource file, the specific wording is as follows:

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <attr name="test1" format="string" />
  4. <declare-styleable name="MyView">
  5. <attr name="TextColor" format="color" />
  6. <attr name="textSize" format="Dimension" />
  7. <attr name="text" format="string" />
  8. </declare-styleable>
  9. </Resources>

Attrs.xml explained as follows
Java code
  1. attr child elements:
  2. Defines a specific property, format, which represents the type of the value of this property, in the following categories:
  3. 1.reference: Refer to the resource ID in the specified theme, this type means that the value you pass can be a reference resource
  4. 2.string: String, if you want to be able to write values directly can also be used similar to "@string/test" reference resources, can be written format="String|reference"
  5. 3.Color: Color
  6. 4. Boolean: Boolean value
  7. 5.dimension: Dimension value
  8. 6.float: float type
  9. 7.integer: Integral type
  10. 8.fraction: Percentage
  11. 9.Enum: Enumeration, if you provide a property that can only be selected by others, can not be passed casually, it could be written like this
  12. <attr name="Language" >
  13. <enum Name="China" value="1"/>
  14. <enum Name="中文版" value="2"/>
  15. </attr>
  16. 10.flag: bit or Operation
  17. Declare-styleable child elements:
  18. Defines a Styleable object, each Styleable object is a collection of attr properties Note: The Name property here is not necessarily the same as the custom class name, just to distinguish the properties of the corresponding class.
  19. Note: The property resource file above defines this property, as to exactly which custom view component to use the property, the property does not belong to the property resource file tube, that is, the property resource file is a public, everyone can use, but for the convenience of management, Typically a custom view property is written as a Declare-styleable collection. The properties defined by the property resource can return what effect, depending on the code implementation of the custom component


Step two: In the custom class, refer to the attribute defined in the Attrs file to set the value for your own property

Java code
  1. Package cn.com.androidtest.ui;
  2. Import CN.COM.ANDROIDTEST.R;
  3. Import Android.content.Context;
  4. Import Android.content.res.TypedArray;
  5. Import Android.graphics.Canvas;
  6. Import Android.graphics.Color;
  7. Import Android.graphics.Paint;
  8. Import Android.graphics.Paint.Style;
  9. Import Android.graphics.Rect;
  10. Import Android.util.AttributeSet;
  11. Import Android.view.View;
  12. Public class MyView extends View
  13. {
  14. private Paint Mpaint;
  15. private Context Mcontext;
  16. private static String mstring;
  17. private String test;
  18. Public MyView (context context)
  19. {
  20. super (context);
  21. Mpaint = new Paint ();
  22. }
  23. Public MyView (Context context,attributeset attrs)
  24. {
  25. super (CONTEXT,ATTRS);
  26. Mpaint = new Paint ();
  27. / * Get declare-styleable set here * /
  28. TypedArray Typearray = context.obtainstyledattributes (Attrs,r.styleable.myview);
  29. / * Remove the corresponding attribute value from the collection, and the second parameter is the default value that the user uses when configuring the property * /
  30. int textcolor = Typearray.getcolor (R.styleable.myview_textcolor,0XFFFFFFFF);
  31. float textSize = typearray.getdimension (r.styleable.myview_textsize, 36);
  32. mstring = typearray.getstring (R.styleable.myview_text);
  33. / * Set your own class member variable * /
  34. Mpaint.settextsize (textSize);
  35. Mpaint.setcolor (TextColor);
  36. / * Close resource * /
  37. Typearray.recycle ();
  38. }
  39. @Override
  40. protected void OnDraw (canvas canvas)
  41. {
  42. Super.ondraw (canvas);
  43. Mpaint.setstyle (Style.fill);
  44. Canvas.drawrect (new Rect (ten, N, N ), mpaint);
  45. Mpaint.setcolor (Color.Blue);
  46. Canvas.drawtext (mstring, Ten, mpaint);
  47. }
  48. }


Step three: Use the custom component and set the properties

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:myandroid="Http://schemas.android.com/apk/res/cn.com.androidtest"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. >
  9. <TextView
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="@string/hello"/>
  13. <Cn.com.androidtest.ui.MyView
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. myandroid:textcolor="#ff0000"
  17. myandroid:textsize="20px"
  18. myandroid:text="http://wujiandong.iteye.com"/>
  19. </linearlayout>


Note: In Java code, the way to take property values, you must set a namespace for the custom component when the XML uses the component [xmlns:myandroid=] http://schemas.android.com/apk/res/ Cn.com.androidtest "], otherwise the component property cannot be set
Namespace notation: xmlns: space name = "Http://schemas.android.com/apk/res/Custom component Package Name"
There is also a place to note when writing a package name:
If your custom view package is similar to the two diagram below, then the package name can only be written as the topmost package [cn.com.androidtest], not [Cn.com.androidtest.ui]






The fourth step: finally finished, look down





2: How to work with XML resource files
Basically the same as the first way, only the Java code to take the property value of the part, and others use your self-righteous view a little different

Java code Syntax:

Java code
  1. /* Resource ID number Reference */
  2. int Resouceid =-1;
  3. /* Get the Resource ID number, first parameter: namespace name. The second parameter: the name of the property set in the XML file. Third parameter: Default value */
  4. Resouceid = Attrs.getattributeresourcevalue (null, "TextColor", 0);
  5. if (Resouceid > 0)
  6. TextColor = Context.getresources (). GetColor (Resouceid);
  7. Resouceid = Attrs.getattributeresourcevalue (null, "TextColor", 0);
  8. if (Resouceid > 0)
  9. mstring = Context.getresources (). GetText (Resouceid, "http://wujiandong.iteye.com"). ToString ();


XML used in the time, the namespace can not, the property name should be corresponding to the good, or the program will not be taken, not like there is an XML resource file in a way with a constraint. Absolute freedom equals no freedom ~ ~
XML code
  1. <Cn.com.androidtest.ui.MyView
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. textcolor="#ff0000"
  5. textsize="20px"
  6. text="http://wujiandong.iteye.com"/>

Properties of the Android app Resource family (Attribute) resource

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.