Android Custom Properties

Source: Internet
Author: User

Android custom properties are often used in our custom controls, and custom attributes can be used in our XML layout files, which reduces the amount of code and increases the robustness and readability of the code. So, before mastering a custom control, you need to know how to customize the properties first.

Let's start by looking at the custom attributes.

There are several types of attributes that can be defined in Values/attrs.xml:

1. Reference: Refer to a resource ID. <attr name = "Background" format = "Reference"/>

2. Color: Colour value. <attr name = "TextColor" format = "Color"/>

3. Boolean: Boolean value <attr name = "focusable" format = "Boolean"/>

4. Dimension: Dimension value. <attr name = "layout_width" format = "Dimension"/>

5. Float: floating-point value. <attr name = "Fromalpha" format = "float"/>

6. Integer: integer value <attr name = "frameduration" format= "integer"/>

7. String: Strings. <attr name = "text" format = "string"/>

8. Fraction: percent <attr name = "Pivotx" format = "fraction"/><attr name = "Pivoty" format = "fraction"/>

9. Enum: enumeration value

<attr name= "Orientation" ><enum name= "horizontal" value= "0"/><enum name= "vertical" value= "1"/>< /attr>

Flag: Bit or operation <attr name= "Windowsoftinputmode" ><flag name = "Stateunspecified" value = "0"/><flag name = "s Tateunchanged "value =" 1 "/></attr>

11. Multi-type. <attr name = "Background" format = "Reference|color"/>

The property name (name) can be arbitrarily named in all of the above attributes. Format is simple enough to indicate what type this property is.

1. Values/attrs.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<declare-styleable name= "Custom_view" >//This is the name of the property set, which is needed when we find the attribute in Java.

<attr name= "custom_id" format= "integer"/> property names and attribute types, and property names can be used in both our XML and Java files

<attr name= "TextSize" format= "Dimension"/>

</declare-styleable>

</resource>

2. Properties found in Java files

TypedArray a = context.obtainstyledattributes (Attrs,r.styleable.custom_view);//r.styleable.custom_view--> Corresponds to the name of the declare-styleable node in our attrs.xml,

MSRC = a.getdrawable (r.styleable.custom_view_textsize);//When you get the property, it's like JSON. Get with different types

3. Referencing in XML

First define the name (app) and the introduction package name (COM.FWL.MYSEEKBAR)/apk/res--> This paragraph also do not forget, you can also directly use Auto

xmlns:app= "Http://schemas.android.com/apk/res/com.fwl.myseekbar"

Use:

App:endvalue= "300"

app--> name, endvalue--> attribute name, 300--> attribute value

These are just a few of the things that introduce custom attributes, and the actual use is a little more deliberate.

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.