1. Write a class that inherits the view or controls (such as TextView) that you want to extend functionality to.
public class CustomView extends View {};
2. Create a Attr.xml file under/res/value.
Without this file the custom control can still be displayed, but only with the inherited parent class contained in the properties, with this file can be added to customize the namespace, to set the custom properties (where format optional values see the end of the text) (you can see here is very abrupt, insist on reading, I believe you will understand here!) )
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "CustomView" >
<attr name= "TextColor" format= "Color"/>
<attr name= "TextSize" format= "Dimension"/>
</declare-styleable>&NBSP;
</resources>&NBSP;
2, CustomView the construction method of overriding the parent class (I generally write three) in the constructor method gets the value of the custom is property.
Public CustomView (context context) {
Super (context);
} When parsing the
//xml file, the tags are parsed into a class, and the attributes and attribute values in the tags are passed into the attributeset, so we'll get the property values from here and set them to the brushes.
Public CustomView (context context, AttributeSet Attrs) {
Super (context, attrs);
Init (context,attrs);
}
Public CustomView (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, defstyle);
Init (context,attrs);
}
private void init (context context, AttributeSet attrs) {
TypedArray array = context.obtainstyledattributes ( Attrs, R.styleable.customview);//Gets the property value of the attribute defined in attr, which is the
int color = Array.getcolor set in the layout used when the control is set to the interface ( R.styleable.customview_textcolor, color.red);//The second parameter is the default value
Float size = array.getdimension (r.styleable.customview _textsize);
Paint = new paint ();
Paint.setcolor (color);
Paint.settextsize (size);
Array.recycle ();//must HAVE!! Clears the original array to prevent subsequent properties from appearing.
}
4. Rewrite the OnDraw () method.
@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Canvas.drawtext ("I was drawn", ten, four, paint);//The X-axis y-axis of the second and third parameters as coordinates
}
5. Create a new activity (no longer given here), add a custom control to the layout file it uses, and you can introduce a custom namespace that uses the properties defined in attr.
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:dinglin= "Http://schemas.android.com/apk/res/cn.itheima.customview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<!--use a custom control in the form of a package name + class name where you can use the properties defined in Attr.xml in the named control DL-
<cn.itheima.customview.customview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Dl:textcolor= "#ff00ff00"
Dl:textsize= "25dip" >
</cn.itheima.customview.CustomView>
</LinearLayout>
Get! Run to see your own defined control, the control is a little ugly, the beginning of the article has been said, just a simple introduction of the custom control development process! Don't Blame It!
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------
The value that the format option can take when Android custom properties
1. Reference: Refer to a resource ID.
(1) Attribute definition:
<declare-styleable name= "name" >
<attr format= "Reference" name= "background"/>
</declare-styleable>
(2) attribute use:
<imageview
Android:layout_width= "42dip"
android:layout_height= "42dip"
android:background= "@drawable/Picture id"/>
2. Color: Colour value.
(1) Attribute definition:
<declare-styleable name= "name" >
<attr format= "Color" name= "TextColor"/>
</declare-styleable>
(2) attribute use:
<textview
Android:layout_width= "42dip"
android:layout_height= "42dip"
Android:textcolor= "#00FF00"/>
3. Boolean: Boolean value.
(1) Attribute definition:
<declare-styleable name= "name" >
<attr format= "boolean" name= "Focusable"/>
</declare-styleable>
(2) attribute use:
<button
Android:layout_width= "42dip"
android:layout_height= "42dip"
Android:focusable= "true"/>
4. Dimension: Dimension value.
(1) Attribute definition:
<declare-styleable name= "name" >
<attr format= "Dimension" Name= "Layout_width"/>
</declare-styleable>
(2) attribute use:
<button
Android:layout_width= "42dip"
android:layout_height= "42dip"/>
5. Float: floating-point value.
(1) Attribute definition:
<declare-styleable name= "Alphaanimation" >
<attr format= "float" name= "Fromalpha"/>
<attr format= "float" name= "Toalpha"/>
</declare-styleable>
(2) attribute use:
<alpha
Android:fromalpha= "1.0"
Android:toalpha= "0.7"/>
6. Integer: Integer value.
(1) Attribute definition:
<declare-styleable name= "Animatedrotatedrawable" >
<attr format= "integer" name= "Frameduration"/>
<attr format= "integer" name= "Framescount"/>
</declare-styleable>
(2) attribute use:
[HTML] View Plaincopyprint?
<animated-rotate
android:frameduration= "100"
Android:framescount= "/>"
7. String: Strings.
(1) Attribute definition:
<declare-styleable name= "Mapview" >
<attr format= "string" name= "ApiKey"/>
</declare-styleable>
(2) attribute use:
<com.google.android.maps.mapview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:apikey= "0jokq80od1jl9c6haja99ugxcris2cgjko_bc_g"/>
8. Fraction: percentage.
(1) Attribute definition:
<declare-styleable name= "Rotatedrawable" >
<attr format= "fraction" name= "Pivotx"/>
<attr format= "fraction" name= "Pivoty"/>
</declare-styleable>
(2) attribute use:
<rotate
android:pivotx= "200%"
android:pivoty= "300%"/>
9. Enum: enumeration value.
(1) Attribute definition:
<declare-styleable name= "Name" >
<attr name= "Orientation" >
<enum name= "Horizontal" value= "0"/>
<enum name= "Vertical" value= "1"/>
</attr>
</declare-styleable>
(2) attribute use:
<linearlayout
android:orientation= "Vertical" >
</LinearLayout>
Flag: Bit or operation.
(1) Attribute definition:
<declare-styleable name= "Name" >
<attr name= "Windowsoftinputmode" >
<flag name= "stateunspecified" value= "0"/>
<flag name= "stateunchanged" value= "1"/>
<flag name= "Statehidden" value= "2"/>
<flag name= "Statealwayshidden" value= "3"/>
<flag name= "statevisible" value= "4"/>
<flag name= "statealwaysvisible" value= "5"/>
<flag name= "adjustunspecified" value= "0x00"/>
<flag name= "adjustresize" value= "0x10"/>
<flag name= "Adjustpan" value= "0x20"/>
<flag name= "adjustnothing" value= "0x30"/>
</attr>
</declare-styleable>
(2) attribute use:
<activity
Android:windowsoftinputmode= "Stateunspecified | stateunchanged | Statehidden ">
</activity>
Note: You can specify multiple types of values when you define a property:
(1) Attribute definition:
<declare-styleable name= "name" >
<attr format= "Reference|color" name= "Background"/>
</declare-styleable>
(2) attribute use:
<imageview
Android:layout_width= "42dip"
android:layout_height= "42dip"
android:background= "@drawable/Picture id| #00FF00"/>
turn from:/http blog.csdn.net/dinglin_87/article/details/7431545
Android Custom Control steps