The role of Typearray and Obtainstyledattribute

Source: Internet
Author: User

The attrs.xml is typically created under a resource file

1.attrs.xml

<?xml version= "1.0" encoding= "Utf-8"?>   <resources>       <declare-styleable name= "button" >           <attr name= "textSize" format= "Dimension"/>       </declare-styleable>   </resources>  If the root tag is a resources, define the variable to have a name, declare-styleable name= "button", where the name is the button. In this name, you can have more than one custom attribute. A property named Textsize is defined, and the format is dimension, which specifies the type of the Textsize property and can only be used to define the font size.

2. The layout file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:myapp= "Http://schemas.android.com/apk/res/com.easymorse.textbutton"android:orientation= "vertical" android:layout_width= "fill_parent"Android:layout_height= "Fill_parent" android:background= "@drawable/background_color" > <linearlayout android:layout_width= "Fill_ ParentAndroid:layout_height= "10dip"/> <linearlayout android:layout_width= "Fill_parent"Android:layout_height= "40dip" > <Com.easymorse.textbutton.TextButton android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"Android:layout_weight= "1" android:text= "movie"android:gravity= "Center_vertical|center_horizontal"Android:background= "@drawable/button" android:focusable= "true"android:clickable= "true" myapp:textsize= "20sp"/>One more xmlns:myappmyapp:textSize in the root tag.= "20SP"

3. Get the value under the program

 Public Textbutton (final  context context, AttributeSet attrs) {     This (context, attrs,0);     TypedArray TypedArray=context.obtainstyledattributes (attrs, R.styleable.button);      this. Settextsize (Typedarray.getdimension (R.styleable.button_textsize,));     Typedarray.recycle ();  TypedArray and Obtainstyledattributes (AttributeSet Attrs,r.styleable.button) 

4.

Here are the custom properties of format, there can be many kinds: referencestringcolordimension Boolean integer float Fraction enum Flag

The role of Typearray and Obtainstyledattribute

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.