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