Android learning notes-custom controls (custom components)

Source: Internet
Author: User
Tags getcolor

In general, the android system provides us with enough controls, but if you want more functions and more personalized controls. In this case, we must "CREATE" our own controls. Of course, there are many ways to customize controls. Here I will share with you the practical method I just met.

First, you need to create an attrs. xml file in the values folder to define the attributes required when the custom control is used in the layout file.

<?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> 

Here I have defined two attributes, whose categories are color and dimension.

Next, we need to create a class, which is the custom control we need. Generally, we inherit an existing control, such as textview and button. In this way, we can inherit their functions and have unique functions. My controls inherit the View class, which is relatively simple.

Package COM. customview; import android. content. context; import android. content. res. typedarray; import android. graphics. canvas; import android. graphics. paint; import android. util. attributeset; import android. view. view; public class myview extends view {private paint mpaint; Public myview (context) {super (context); // todo auto-generated constructor stubmpaint = new paint ();} public myview (context Conte XT, attributeset attrs) {super (context); // typedarray is a container that can hold our own or system-defined attributes. Typedarray A = context. obtainstyledattributes (attrs, R. styleable. myview); // obtain the textcolor attributes in myview that we need. These attributes are defined in values/attrs. the // attribute in the XML file belongs to the color category, so it must be obtained using the getcolor method. Int textcolor = A. getcolor (R. styleable. myview_textcolor, 0 xffffffff); // obtain the textsize attribute in myview. This attribute category belongs to dimension, so it must be obtained through the getdimension method. // If it is not used in the layout file, 36 float textsize = A is returned. getdimension (R. styleable. myview_textsize, 36); mpaint = new paint (); mpaint. settextsize (textsize); mpaint. setcolor (textcolor); // The method used after the typearray object is used. Re-obtain the attributes for later use. A. Recycle ();}/*** the most important method for customizing controls. here we need to "Draw" the desired controls based on the attributes. * // @ Overrideprotected void ondraw (canvas) {// todo auto-generated method stubsuper. ondraw (canvas); canvas. drawtext ("this is my first custom control", 100,100, mpaint );}}

The comments in the code should be clear. I will not detail them here.

Finally, we can use it in the layout file. Of course, we can also use it directly in the Code. (I don't like this. Why don't we use a simple method ?).

<?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/com.customview"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    >    <com.customview.MyView        android:id="@+id/my_cuscom_view"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        test:textSize="20dp"        test:textColor="#fff"        /></LinearLayout>

Note that xmlns: test = "http://schemas.android.com/apk/res/com.customview" is useful? Before for this problem, I am also very tangled, do not understand why always want to add xmlns in XML file: Android = "http://schemas.android.com/apk/res/android" this sentence (below is only a thin, I don't understand such a small problem). I found the answer on the Internet. In fact, this is to replace the following things with Android. In this way, press Ctrl + after we enter Android: XX.
. I don't know if this answer is correct. The usage of the sentence we mentioned at the beginning is the same. The end is the package name of the project. Of course, text can be replaced by any string.

Such a simple control is generated.

Finally, add the Code. This is an example I wrote.

Click Open Link

Related Article

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.