"Android" Typedarray and Obtainstyledattributes use (R.STYLEABLE.XX_XX)

Source: Internet
Author: User

Original: http://www.cnblogs.com/rayray/p/3382087.html

On the basis of writing the Android custom button example, if you want to specify the font size to produce this effect:

In fact, you do not need to customize variables, you can directly use the configuration properties of TextView:

<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" android:textsize= "22SP"/>

Here the size of the font used the SP, here to say the difference between SP and DP (DIP), dip is:

DP density-independent Pixels–an Abstract unit This is based on the physical density of the. These units are relative to a 160 dpi screens, so one DP is one pixel on a 160 dpi screen. The ratio of dp-to-pixel would change with the screen density and but not necessarily in direct proportion. Note:the compiler accepts both "Dip" and "DP", though "DP" is more consistent with "SP".

However, if it is related to fonts, it is best to use the SP, similar to the DP, but more appropriate to the font size:

SP Scale-independent Pixels–this is like the DP unit, but it's also scaled by the user's font size preference. It is recommend to specifying font sizes, so they'll be adjusted for both the "screen density" and use R ' s preference.

Here to demonstrate the use of custom variables, font size uses custom properties instead.

First you create a variable, you create a values/attrs.xml file, and the file name is arbitrary, but in the values directory:

<?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 resources, the defined variable must have a name, declare-styleable name= "button", where the name button is defined. In this name, you can have more than one custom attribute. Defines a property named Textsize, the format is dimension, which specifies the type of the Textsize property and can only be used to define the font size.

To assign a value through a custom attribute in a layout file:

<?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_parent"
android: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"/>

This is added to the root tag:

Xmlns:myapp=http://schemas.android.com/apk/res/com.easymorse.textbutton

Declared MyApp this name space, MyApp is any name, you can casually named, behind the:

http://schemas.android.com/apk/res/

is fixed. followed by the application of the package name.

In the following custom button: Myapp:textsize, is to use the <attr name= "textsize" This variable, to assign a value to the variable.

A process is also needed to get this assignment in the program:

Public Textbutton (final context, AttributeSet Attrs) {
This is (context, attrs, 0);
TypedArray typedarray=context.obtainstyledattributes (Attrs, R.styleable.button);
This.settextsize (Typedarray.getdimension (r.styleable.button_textsize, 15));
Typedarray.recycle ();

Where the Typedarray instance is a container of properties, the Context.obtainstyledattributes () method returns. AttributeSet is a collection of attributes for a node, in this case a collection of properties in the <com.easymorse.textbutton.textbutton node.

This sentence:

Typedarray.getdimension (R.styleable.button_textsize,
15)

The value of the custom textsize is obtained, and if not, the default value is used, 15.

Finally, don't forget to call:

Typedarray.recycle ();

function is:

Give back a previously retrieved styledattributes, for later re-use.

There can be many types of custom attribute format here: Reference string color dimension boolean integer float fraction enum flag

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.