A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
If you should be good enough, Look At This http://www.gfan.com/dev/android/guide/topics/ui/custom-components.html, the official website is really powerful
In Android, your applications have a fixed connection with the view components, such as buttons, textviews, and edittext ), list box (listview), check box (checkbox), single button (radiobutton), scroll bar (Gallery), spinner ),
There are also some advanced view components with special purposes, such as autocompletetextview, imageswitcher, and textswitcher. In addition, a wide range of linear layout (linearlayout), Framework layout (framelayout ),
Such layout components are also considered view components, which are derived from the View class.
Your application displays these control components and layout components on the screen in a certain way. Generally, these components are enough for you, however, you should also know that you can use class inheritance to create your own components. Generally, You can inherit components such as view and layouts (layout components, it can even be some advanced control components. Next we will explain why inheritance is required:
To achieve a certain goal, you may need to expand an existing view component. The following examples are used to teach you how to do this.Content:
The following steps are summarized to teach you how to create your own components:
on', Such as ondraw (), onmeasure (), and onkeydown.
The inheritance class can be defined in activities so that you can conveniently call it, but this is not necessary (maybe you want to create a component that everyone can use in your application ).Full custom components (fully customized components)
A Fully customized component can be used to create graphical components for display. It may be a graphical meter like a voltmeter, or a ball that shows the lyrics in karaoke will scroll with music. In either way, you cannot simply use the combination of components, no matter how you combine these existing components.
Fortunately, you can easily create your own components as required, you will find that your imagination, screen size, and processor performance are insufficient (remember that your application will only run on platforms with lower performance than your desktop ).
The following describes how to create a fully custom component:
onMeasure()Function, so you have to reload
onDraw()Function. When both functions use the default one
onDraw()The function will not do anything, and the default
onMeasure()The function automatically sets a 100x100-size, which may not be what you want.
on...All functions must be rewritten once.
onDraw()The function will pass you a canvas object, through which you can do anything on a two-dimensional graph, including some other standard and common components and text formats, anything you can think of can be implemented through it.
Note:This does not include 3D graphics. If you want to use 3D graphics, you should change your parent class from view to surfaceview class and use a separate thread. See glsurfaceviewactivity
onMeasure()Functions are tricky, because this function is a key part of the interaction between components and containers,Onmeasure ()It should be reloaded so that it can effectively and accurately express the measured values of the part it contains. This is a bit complicated, because we should not only consider the restrictions of the parent class (throughOnmeasure ()). At the same time, we should know that once the width and height of the measurement come out, it should be called immediately.Setmeasureddimension ()Method.
To sum up, execute
onMeasure()Functions are divided into several stages:
onMeasure（）The method is called, and the height and width parameters also involve (
heighMeasureSpecBoth parameters are Integer type), and you should consider the size limit of your product. For more information, see view. onmeasure (INT,
onMeasure（）Calculate the necessary measurement length and width to display your component. It should be consistent with the specification, although it can implement features other than the specification (in this example, what can the parent class choose to do, including cutting, sliding, submitting exceptions, or calling again with different parameters?
setMeasuredDimension(int width, int height)Otherwise, an exception occurs.
In API demos, customview provides an example of a custom component, which is defined in the labelview class.
The labelview example involves all aspects of custom components:
onMeasure（）(Note: In labelview, a private function is used to determine the component size.
onDraw()The function displays the lable on the provided canvas.
In this example, you can use custom_view_1.xml to view the usage of the custom component labelview. In XML files, note that
app:Mixed use of two parameters,
app：Parameters indicate that the application is considered an individual of the labelview component, which will also be defined as resources in the r class.
If you do not want to create a fully custom component, but a new component is generated by a combination of several existing components, hybrid component technology is more suitable. To put it simply, combining several existing components into a logical combination can be encapsulated into a new component. For example, a combo box component can be seen as a mixture of edittext and a button component with a pop-up list. If you click the button to select an item for the list,
In Android, there are actually two other view classes that can achieve similar effects: spinner and autocompletetextview, but combo
Box is easier to understand as an example.
The following describes how to create a composite component:
onMeasure（）Method, because layout has a better default processing. However, you can reload it if you think it is necessary.
onSeries functions, such
onKeyDown()You can press a key to select the corresponding value in the list.
In short, the layout class as the base class has the following advantages:
onMeasure()There is no need to reload functions. The two functions have already done well.
In the API demos project, there are two examples of List classes in the API demos project: Example 4 and Example 6. The speechview component in it is derived from the linearlayout class, the original code is
In some cases, you may have a simpler way to create your components. If you have a very similar component, all you have to do is simply derive your component from this component, focusing on some of the necessary modifications. You can use the same method to completely customize components, but you can simply obtain some existing processing mechanisms by deriving new components from the view, these are probably what you want, and there is no need to start from scratch.
For example, there is a notepad application in the SDK ). This example demonstrates many practical details of the Android platform. For example, you will learn how to derive a notepad that can automatically wrap lines from editview. This is not a perfect example, because these APIs have changed a lot compared to earlier versions, but it does illustrate some problems.
If you haven't checked the program, you can now import the notepad routine in eclipse (or view the source code only through the provided link ). Especially in noteeditor. Java
Note the following:
This class is defined by the following code line:
public static class MyEditText extends EditText
NoteEditor activityClass, but it is a common (public), So if necessary, it can
staticClass (static class), which means that the so-called "virtual state method" for accessing data through the parent class will not appear, so that the class can become a non-seriously dependent
NoteEditor. This is a clear idea for creating inline classes that do not need to be accessed from external classes. It ensures that the generated classes are small and can be conveniently called by other classes.
EditTextClass extension, which is the parent class we choose to use to customize. After this is done, the new class can be used as a common
Generally, the parent class is called first. Furthermore, this is not a default constructor, but a constructor with parameters. Because
EditTextIs created using parameters extracted from the XML layout file. Therefore, our constructor also needs to retrieve parameters and pass these parameters to the parent class.
In this example, only
onDraw()Reload a method. However, you can easily reload other required methods for your custom components.
For the notepad example
canvas) Draw blue lines (
canvasThe class is rewritten.
onDraw()Method transfer ). This function will be called when it is about to end
super.onDraw()Function. The method of the parent class should be called, but in this example, we call it after we have drawn the blue line.
Now we already have our own custom components, But how should we use them? In the notepad example, the custom components are directly used in the predefined layout file. Let's take a look.
<view xmlns:android="http://schemas.android.com/apk/res/android" class="com.android.notepad.NoteEditor$MyEditText" id="@+id/note"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@android:drawable/empty"android:padding="10dip"android:scrollbars="vertical"android:fadingEdge="vertical" />
NoteEditor$MyEditTextThis is the standard method that references inline classes in Java programming.
This is all you need to do. It is a simple example. But the key to the problem is: how complicated your requirements are, how complicated your custom components are.
A more complex component may need to overload more
onSeries functions, and many special functions are required to fully implement the functions of custom components. The only limit is your imagination and what work you need components to perform.
As you can see, Android provides a sophisticated and powerful component model that allows you to do your work as much as possible. From simple component adjustment to component mixing, or even completely custom components, you can use these technologies flexibly to get an android program that fully meets your appearance requirements.
Start building with 50+ products and up to 12 months usage for Elastic Compute Service