How to use Typedarray when Android custom attributes

Source: Internet
Author: User

Sometimes the traditional Android page layout is not enough to meet our needs, often need to define their own view, often inherit the view, and then rewrite the construction method and OnDraw functions, and then implement their own definition of complex view. We know that when assigning properties to a control, it is common to use the properties of the Android system, such as android:layout_height= "Wrap_content", in addition, we can also define the attributes, so that when we use the shape of the Myapp:mytextsize= "20SP" in the way, the steps are roughly as follows:1 Create a Attr.xml file under Project file Res/value that contains several attr collections, for example: [Html]<?xml version= "1.0" encoding= "Utf-8"?> < Resources><declare-styleablename= "MyView"><attr name= "Mytextsize" format= "Dimension"/> <attr name= "mycolor" format= "Color"/> </declare- Styleable> </resources> where resource is tagged, you can define a number of declare-styleable,<declare-styleable name= "MyView" in the inside The name of the variable is defined in >, and a number of properties can be customized below, for <attr name= "mytextsize" format= "Dimension"/>, the name of the property is "Mytextsize", format specifies that the property type is dimension and can only represent the size of the font. Format can also specify other types such as; reference represents a reference, reference to a resource idstring represents a string color represents a value dimension represents a dimension value Boolean represents a Boolean value integer represents an integer value Floa T means that the floating-point value fraction represents the Percent enum represents the enumeration value flag represents the bitwise Operation 2 Type the following line in the layout file that is used to the custom view: Green is the prefix name of the property you define, and pink is the item's package name, so that the properties of the view that we define , you can use the properties you have defined in attr, for example: [HTML] <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"  &NBSP; Xmlns:myapp = "Http://schemas.android.com/apk/res/com.eyu.attrtextdemo" &NBSP; xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "android:orientation=" vertical "tools:context=". Mainactivity "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_c Ontent "android:text=" @string/hello_world "/> <com.eyu.attrtextdemo.myview Android:layout_hei ght= "Wrap_content" android:layout_width= "wrap_content" myapp:mytextsize= "20SP"      myapp:mycolor= "#324243"/></LinearLayout>  m Yapp: mycolor= "#324243" prefix must be followed by xmlns:myapp= "/http/ Schemas.android.com/apk/res/com.eyu.attrtextdemo", These two places must be the same, after this address is http://schemas.android.com/apk/res/Plus package name

3 introduce a custom attribute in the code for custom view, modify the constructor context by calling the Obtainstyledattributes method to get a typearray, Then the Typearray to set the properties Obtainstyledattributes method has three, we most commonly used is a parameter of Obtainstyledattributes (int[] attrs), The parameters are obtained typedarray a = Context.obtainstyledattributes (Attrs,r.styleable.myview) directly styleable, and the Recycle () method must be called after the call is finished. Otherwise this setting will affect the use of the next   specific as follows: [Java] package Com.eyu.attrtextdemo;    import Android.content.Context;  import Android.content.res.TypedArray;  import Android.graphics.Canvas;  import Android.graphics.Paint;  import Android.graphics.Paint.Style;  import Android.util.AttributeSet;  import Android.view.View;    public class MyView extends view{     public paint paint;      &nbs P Public MyView (context context, AttributeSet attrs) {         Super (context, attrs);          paint = new paint ();           // R.styleable.myview This myview is based on <declare-styleablename= "MyView"> and take        TypedArray a = Context.obtainstyledattributes (Attrs,r.styleable.myview);              int textcolor = A.getcolor (R.styleable.myview_mycolor, 003344);          Float textSize = a.getdimension (r.styleable.myview_mytextsize, 33);          paint.settextsize (textSize);          Paint.setcolor (TextColor);          a.recycle ();             Public MyView (context context) {         s Uper (context);         //TODO auto-generated constructor stub     }             @Override   www.2cto.com    protected void OnDraw (canvas canvas) {        //TODO auto-generated method stubs          Super.ondraw (canvas);       &NBsp     Paint.setstyle (Style.fill);          Canvas.drawtext ("Aaaaaaa", ten, +, paint);                         }  

How to use Typedarray when using Android custom properties

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.