In the development process, although the Android system provides a lot of control for us to use, but still can not meet our human needs, feel that we are indeed greedy, hehe! At this point, we're probably going to use custom controls, and custom attributes, how do we do that?
Generally follow these steps:
1, inherit view or other controls, override functions such as constructor Ondraw,onmeasure,ontouch.
2, custom attributes, you need to create attrs.xml under values, in which you need to define the properties, detailed property types can refer to the article http://www.jb51.net/article/32172.htm.
3, the use of the XML layout file to the custom view needs to include xmlns: prefix = "http://schemas.android.com/apk/res/Custom View package path", as I use the prefix is Xmlns:app. When using a custom attribute, the prefix is also used: the property name.
Here are a few examples of steps:
----------define view, override OnDraw ()---------------
public class DefaultView extends View {
/** brush paint, using and setting the color and style of the drawing pattern */
Private Paint Mpaint;
/** Construction Method */
Public DefaultView (Context context) {
Super (context);
}
Public DefaultView (context context, AttributeSet Attrs) {
Super (context, attrs);
Mpaint = new Paint ();
An array of/**typedarray used to hold properties obtained by context.obtainstyledattributes
* After use, be sure to call the Recycle method
*/
TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.defaultview);
/** get the color value, otherwise use the default value */
int textcolor = Array.getcolor (R.styleable.defaultview_textcolor, 0xff00ff00);
/** get text size, otherwise use default value */
float textSize = array.getdimension (r.styleable.defaultview_textsize, 36);
Mpaint.setcolor (TextColor);
Mpaint.settextsize (textSize);
/** If you do not call the Recycle () method, this setting will have an impact on the next use */
Array.recycle ();
}
/** rewrite the OnDraw () method, the pattern to be drawn is implemented here */
public void OnDraw (canvas canvas) {
Super.ondraw (canvas);
/** Brush Color */
Mpaint.setcolor (color.red);
/** Brush Style */
Mpaint.setstyle (Style.fill);
/** Brush Width */
Mpaint.setstrokewidth (2);
/** Draw Rectangle */
Canvas.drawrect (Ten, ten, N, Mpaint);
Mpaint.setcolor (Color.Blue);
/** setting brush anti-heavy teeth */
Mpaint.setantialias (TRUE);
/** Drawing Text */
Canvas.drawtext ("The painting is the text", ",", mpaint);
Mpaint.setcolor (Color.green);
/** Draw Round */
Canvas.drawcircle (100f, 100f, 50f, Mpaint);
}
}
-------Create a Attrs.xml file under the Res/values folder to customize the properties of our view------------------------
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "DefaultView" >
<attr name= "TextColor" format= "Color"/>
<attr name= "TextSize" format= "Dimension"/>
</declare-styleable>
</resources>
----------used in our layout files----------
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
xmlns:app= "Http://schemas.android.com/apk/res/com.ldm.map" <!--to add this phrase-
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<com.ldm.map.defaultview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
App:textcolor= "#f0f0f0" <!--prefix is app:-->
App:textsize= "16sp"/>
</LinearLayout>
Custom View Simple Small example