This topic describes how to customize a view.
1. Custom View
For beginners, they are used to the traditional page layout method of Android. The Code is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
However, the layout method above can help us develop simple applications. However, if you want to write a complex application, it is a little far-fetched. If you do not believe it, you can study the source code, the layout of high handwriting is usually written as follows:
<?xml version="1.0" encoding="utf-8"?> <A> <B></B> </A> <?xml version="1.0" encoding="utf-8"?><A><B></B></A>
A extends linerlayout and B extends textview.
To help you better understand it, I wrote a simple demo. The specific steps are as follows:
First, create an android project named viewdemo.
Then customize a View class named myview (extends view). The Code is as follows:
Public class myview extends view {private paint mpaint; private context mcontext; Private Static final string mstring = "welcome to Mr Wei's blog"; Public myview (context) {super (context); log. V ("myview (context)", "" + this. getheight () + "" + this. getwidth ();} public myview (context, attributeset ATTR) {super (context, ATTR); log. V ("myview (context, attributeset ATTR)", "" + this. getheight () + "" + this. getwidth () ;}@ override protected void ondraw (canvas) {// todo auto-generated method stub super. ondraw (canvas); log. V ("ondraw (canvas)", "" + this. getheight () + "" + this. getwidth (); mpaint = new paint (); // obtain the resource resources rec = getresources (); // inputstream gets the resource stream inputstream in = rec. openrawresource (R. drawable. ic_launcher); // bitmapdrawable parses the data stream bitmapdrawable = new bitmapdrawable (in); // obtain the image Bitmap bitmap = bitmapdrawable. getbitmap (); // draw the image canvas. drawbitmap (bitmap, 100,100, mpaint); // sets the paint brush color mpaint. setcolor (color. red); // set to fill mpaint. setstyle (style. fill); // draw a rectangle. The first two are the coordinates in the upper left corner of the rectangle, and the last two are the coordinates in the lower right corner of the rectangle canvas. drawrect (New rect (10, 10,100,100), mpaint); mpaint. setcolor (color. blue); // draw the text canvas. drawtext (mstring, 10,110, mpaint);} protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {// todo auto-generated method stub super. onmeasure (widthmeasurespec, heightmeasurespec); log. V ("onmeasure", "" + this. getheight () + "" + this. getwidth ());}}
Add the custom View to the main. xml layout file. The Code is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <demo.xuxm.com.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
The final execution result is as follows:
2. Use of custom attributes (ATTR. XML, typedarray) in Android
Defining control attributes in XML files is a habit of Android: attrs = "". Can we define our own attributes, such as test: attrs =? The answer is yes.
Well, I will not turn to the topic. I will go directly to the topic. Perform the following steps:
1. Define an attrs. xml file under the Res/values file. The Code is as follows:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> </resources>
2. In myview. the Java code is modified as follows. The following constructor is the focus. We get the defined attribute R. sytleable. myview_textcolor. The default value (floattextsize =. getdimension (R. styleable. myview_textsize, 36);) to prevent definition in the XML file. to use the default value!
Obtain the name of myview, which is defined in <declare-styleablename = "myview"> </declare-styleable>. You can connect the obtained attributes with the name_attribute. typedarray is usually called at the end. the recycle () method. To maintain future consistency with this attribute!
Public class myview extends view {private paint mpaint; private context mcontext; Private Static final string mstring = "welcome to Mr Wei's blog"; Public myview (context) {super (context);} public myview (context, attributeset ATTR) {super (context, ATTR); mpaint = new paint (); typedarray A = context. obtainstyledattributes (ATTR, R. styleable. myview); int textcolor =. getcolor (R. styleable. myview_textcolor, 0 xffffffff); float textsize =. getdimension (R. styleable. myview_textsize, 36); mpaint. settextsize (textsize); mpaint. setcolor (textcolor);. recycle () ;}@ override protected void ondraw (canvas) {// todo auto-generated method stub super. ondraw (canvas); // you can specify whether to fill mpaint. setstyle (style. fill); // draw a rectangle. The first two are the coordinates in the upper left corner of the rectangle, and the last two are the coordinates in the lower right corner of the rectangle canvas. drawrect (New rect (10, 10,100,100), mpaint); mpaint. setcolor (color. blue); // draw the text canvas. drawtext (mstring, 10,110, mpaint );}}
3. Add the custom myview to the layout main. xml file. Use the custom attributes equally. The Custom Attributes must be added:
Xmlns: test = "http://schemas.android.com/apk/res/demo.xuxm.com" Blue is the prefix of custom properties, red is our package name.
The code for Main. XML is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:test="http://schemas.android.com/apk/res/demo.xuxm.com" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <demo.xuxm.com.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" test:textSize="20px" test:textColor="#fff" /> </LinearLayout>
4. The running effect is as follows:
References:
Android advanced tutorial
Research on inheriting view in Android (1)