Inherit ViewGroup Custom view: Steps, Attrs.xml, TypedArray

Source: Internet
Author: User


Date: December 22, 2015 19:01:46
to customize the view implementation steps:

1. Write a custom control class that is the implementation of your custom control.

2. Create a Attrs.xml file in the Res/values directory to add a custom attribute definition to the control in this file.

3. Use the constructor of the class with the AttributeSet parameter, and in the constructor, connect the variables in the custom control class to the properties in the Attrs.xml.

4. Use these already connected property variables in the custom control class.

5. Define the custom control class to the XML file used in the layout.

6. Creating this custom control class object in the interface completes the creation and use of custom controls.



Precautions : 1, define a attrs.xml, the style is:
     <?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "xxx" ><attr name= "xxx" format= "xxx"/><attr name= "xxx" format= "xxx"/>
<attr name= "xxx" format= "xxx"/>
</declare-styleable>
 </RESOURCES>

format type:
"Reference"//reference
"color"//color
"Boolean"//Boolean
" Dimension "//Dimension value
" float "//float value
" integer "//Integer value
"string"//String
"fraction"//percentage, e.g. 200%

Ps:format using reference, for example background property may be reference|color,reference represents the content specified by this property instead of the attribute is in XML     Implemented in the file or in the code.

2, create custom view class:

1) in most cases a custom view needs to inherit view or its subclasses 。
2) Construction method: In most cases, if there is no custom attribute can directly use the constructor method of the context, if there is a custom parameter is to use the parameters with context and Attribut         the construction method of ESET.

3) when you define the font size of a control in a custom view class, be aware that float is not an int.
     4) TypedArray ta = context.obtainstyledattributes (attribuset attr,styleableres int[] res);
The custom property mapping is hit in attr and gets to the Typedarray object, typedarray similar to map using key-value pairs, which are
The name of the sytleable name is underlined (_) and the attribute is defined as:
Lefttextcolor = Ta.getcolor (r.styleable.mytopbar_lefttextcolor,0);
5) Typedarray need to release ta.recycle () after use;
6) To add a property to the view in the custom view after getting the attribute, assume that the custom view has two aligned buttons and a centered TextView, then the button and TextView object is first generated (Button BTN = n EW Button (context) |                   TextView TV = new TextView (context)); 7) After acquiring the object of the View Neutron view, add properties for the child view object, for example: Btn.settextcolor (Lefttextcolor);
8) After the child view gets to the attribute, think about how to place the child view in the specified location? Use Layoutparams for position emission, set the width and height of the control when initializing the layoutparams, add other rule with addrule (), add the control to the view using the AddView () method after the addition is complete.
9) When XML references a control you create, you need to configure xmlns:Xmlns:custom="Http://schemas.android.com/apk/res-auto";
10) In the XML, define a id:android:id= "@+id/xxx" for the control, and then obtain the corresponding object in the mainactivity. 11) You can set the callback for event handling on a custom control in a custom control, or you can set the visibility of the component in it.     



From for notes (Wiz)

Inherit ViewGroup Custom view: Steps, Attrs.xml, TypedArray

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.