Android Custom View properties (Atts.xml,typedarray) Learning

Source: Internet
Author: User

is an array container for storing recovery obtainstyledattributes (AttributeSet, int[], int, int) or values, and obtainAttributes(AttributeSet, int[])   must call the Recycle () method when the operation is complete. The index value used to retrieve the Obtainstyledattributes property in the corresponding position of the structure.

To use this class, create a: Atts.xml file under the Valuse folder:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <declare-styleable name="Flowindicator">
  4. <attr name="Count" format="integer" />
  5. <attr name="Space" format="Dimension" />
  6. <attr name="point_size" format="Dimension" />
  7. <attr name="Point_seleted_color" format="color|reference" />
  8. <attr name="Point_normal_color" format="color|reference" />
  9. <attr name="Point_radius" format="Dimension" />
  10. </declare-styleable>
  11. </Resources>


First, declare the custom <declare-styleable name= "Flowindicator" >,nameflowindicator, set the property to a simpler format, the preceding parameter name, followed by the parameter format.

The format of a custom attribute can be as follows:

    • Reference
    • String
    • Color
    • Dimension
    • Boolean
    • Integer
    • Float
    • Fraction
    • Enum
    • Flag

Then use this:

[Java]View Plaincopy
  1. Public Flowindicator (context context, AttributeSet Attrs) {
  2. Super (context, attrs);
  3. //Get examples
  4. TypedArray Typearray = context.obtainstyledattributes (Attrs,
  5. R.styleable.flowindicator);
  6. //Get the corresponding value from Typearray, the second parameter is the default value, as the first parameter is not defined in Atts.xml, returns the second parameter value
  7. Count = Typearray.getinteger (R.styleable.flowindicator_count, 4);
  8. Space = Typearray.getdimension (R.styleable.flowindicator_space, 9);
  9. Radius = typearray.getdimension (R.styleable.flowindicator_point_radius, 9);
  10. Point_normal_color = Typearray.getcolor (
  11. R.styleable.flowindicator_point_normal_color, 0x000000);
  12. Point_seleted_color = Typearray.getcolor (
  13. R.styleable.flowindicator_point_seleted_color, 0xffff07);
  14. int sum = Attrs.getattributecount ();
  15. if (constans.debug) {
  16. String str = "";
  17. For (int i = 0; i < sum; i++) {
  18. String name = Attrs.getattributename (i);
  19. String value = Attrs.getattributevalue (i);
  20. str + = "Attr_name:" + name + ":" + value + "\ n";
  21. }
  22. LOG.I ("attribute", str);
  23. }
  24. Typearray.recycle ();
  25. }

Finally, be sure not to forget Typearray.recycle ():

[Java]View Plaincopy
    1. Give back a previously retrieved styledattributes, for later re-use.

Give back the previously extracted styledattributes, and then use it later.

It should be noted that the Flowindicator_count in the R.styleable.flowindicator_count used when acquiring the property is the format of the name _ property taken.

Once you have defined the custom properties, you can set the properties in the custom control:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <framelayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="Http://schemas.android.com/apk/res/com.dream.myqiyi"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7. <com.dream.myqiyi.widget.FlowIndicator
  8. android:id="@+id/myview"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginbottom="5dip"
  12. app:count="4"
  13. android:gravity="center"
  14. app:point_normal_color="#45000000"
  15. app:point_radius="3dip"
  16. app:point_seleted_color="#ffffff"
  17. app:point_size="5dip"
  18. app:space="10dip" />
  19. </framelayout>


First, make a statement:

Xmlns:app= "Http://schemas.android.com/apk/res/com.dream.myqiyi", "Com.dream.myqiyi" This is the package name of your project.

Then we can use the app: This sets the custom properties.

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
      1. app:point_ Normal_color= "#45000000" &NBSP;&NBSP;
      2.             app:point_radius= "3dip" &NBSP;&NBSP;
      3.            app: Point_seleted_color= "#ffffff" &NBSP;&NBSP;
      4.             app:point_size=" 5dip "&NBSP;&NBSP;
      5.             app:space= "10dip"    

Android Custom View properties (Atts.xml,typedarray) Learning

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.