Android custom Controls (i) translation of official documents

Source: Internet
Author: User
<span id="Label3"></p><p align="left"><p align="left"><strong>Build a custom Component</strong></p></p><p align="left"><p align="left">In android, your application program has a fixed connection to the view class component, such as a button, text box (TextView), editable text box (EditText), list box (ListView), check box (checkbox), Radio Box (RadioButton), ScrollBar (Gallery), spinner (Spinner), etc., There are some more advanced view components with special purpose, such as autocompletetextview,<span class="Apple-converted-space"> Imageswitcher and <span class="Apple-converted-space">Textswitcher. In addition, a wide variety of <span class="Apple-converted-space">linear layouts (linearlayout),<span class="Apple-converted-space"> frame layouts (framelayout), such as layout components (layouts), are also considered as view components, which derive from the view class. </span></span></span></span></p></p><p align="left"><p align="left">Your application is that these control components and layout components are displayed on the screen in a way that is generally sufficient for you, but you should also know that you can create your own components through class inheritance, and you can generally inherit components like view, Layouts (layout component), Can even be some of the more advanced control class Components. Now let's talk about why we should inherit:</p></p><p align="left"><p align="left">· You can create a completely custom style component for implementing a function, such as creating a control component with two-dimensional graphics to control the sound, just like an electronic control.</p></p><p align="left"><p align="left">· You can combine several components to form a new component, your component may contain a ComboBox (a list of text that can be entered) and Dual-pane selector control (two list windows, You can assign the dependencies of each of the windows), and so On.</p></p><p align="left"><p align="left">· You can create your own layout components (layouts). The layout components in the SDK already provide a range of options for you to build your own applications, but advanced developers will find it necessary to develop new layout components based on existing layout components, or even to develop new components entirely from the ground up.</p></p><p align="left"><p align="left">· You can override the display or functionality of an existing Component. For example, change the way the EditText (editable Text) component is displayed on the screen (refer to the Notepad example, which teaches you how to create an underlined display page).</p></p><p align="left"><p align="left">· You can capture events such as pressing keystrokes, and handle these events in some common way (an example of a game).</p></p><p align="left"><p align="left">In order to achieve some kind of goal you may need to expand an existing view component, Let's combine some examples to show you how to do it.</p></p><p align="left"><p align="left"><strong>Content:</strong></p></p><p align="left"><p align="left"><strong>Basic Method (the Approach)</strong></p></p><p align="left"><p align="left"><strong>Fully customizable component (Fully customized Components)</strong></p></p><p align="left"><p align="left"><strong>Examples of custom components (customized Component Example)</strong></p></p><p align="left"><p align="left"><strong>Component blending (or blending of control classes) (Compound components (or Compound CONTROLS))</strong></p></p><p align="left"><p align="left"><strong>Modifying existing Components (tweaking an Existing Component)</strong></p></p><p align="left"><p align="left"><strong>Summary (Go Forth and Componentize)</strong></p></p><p align="left"><p align="left"><strong>Basic Method (the Approach)</strong></p></p><p align="left"><p align="left">Here are a few steps to summarize and teach you how to create your own components:</p></p><p align="left"><p align="left">1. Let your class inherit an existing view<span class="Apple-converted-space"> class or a subclass of VIEW. </span></p></p><p align="left"><p align="left">2. Some methods of overloading the parent Class: the parent class method that requires overloading typically begins with ' on ', such as OnDraw (), onmeasure (), and <span class="Apple-converted-space">onKeyDown (), and so On. </span></p></p><p align="left"><p align="left">O This also applies in the Activity<span class="Apple-converted-space"> <span class="Apple-converted-space"> or listactivity derivation, you need to reload some life cycle functions and some other functional hook functions. </span></span></p></p><p align="left"><p align="left">3. Use your inheritance class: once your inheritance class is created, you can use your inheritance class where the base class can be used, but the completion function is written by yourself.</p></p><p align="left"><p align="left">Inheriting classes can be defined in activities so that you can easily invoke them, but this is not necessary (perhaps in your application you want to create a component that everyone can use).</p></p><p align="left"><p align="left"><strong>Fully customizable component (Fully customized Components)</strong></p></p><p align="left"><p align="left">The method of fully customizing the component can create some graphical components for display (graphical component), perhaps a graphic meter like a voltmeter, or a small ball that displays lyrics inside a karaoke bar as the music scrolls. Either way, you can't simply use the combination of components, no matter how you combine these existing components.</p></p><p align="left"><p align="left">fortunately, you can easily create your own components with your own requirements, and you'll find that it's just your imagination, the size of your screen, and the performance of your processor (remember that your app will only run on platforms that perform less than your desktop).</p></p><p align="left"><p align="left">Here's a quick introduction to creating fully customizable components:</p></p><p align="left"><p align="left">1. The parent class of the most common view class is undoubtedly the view class, so at first you will create a subclass based on this class.</p></p><p align="left"><p align="left">2. You can write a constructor to extract attributes and parameters from an XML file, and you can also define these properties and parameters yourself (perhaps the color and size of the graph meter, or the width and amplitude of the pointer, etc.)</p></p><p align="left"><p align="left">3. You may have to write your own event listeners, access the properties and modify the functions of the function and some of the components themselves on the Code.</p></p><p align="left"><p align="left">4. If you want the component to display something, you are likely to overload the Onmeasure () function, so you will have to overload the OnDraw () Function. When two functions are used by default, then the OnDraw () function will not do anything, and the default onmeasure () function automatically sets a 100x100-size, which may not be the size you want.</p></p><p align="left"><p align="left">5. Other necessary overloads on ... The series functions need to be re-written once.</p></p><p align="left"><p align="left"><strong>OnDraw () and Onmeasure ()</strong></p></p><p align="left"><p align="left">The OnDraw () function will pass you a <span class="Apple-converted-space">Canvas<span class="Apple-converted-space"> object that allows you to do anything on a two-dimensional graphic, including some other standard and generic components, text formats, and anything you can think of that can be done. </span></span></p></p><p align="left"><p align="left"><em>note:</em> <span class="Apple-converted-space">This does not include three-dimensional graphics. if you want to use three-dimensional graphics, you should change your parent class from view to Surfaceview class and use a separate thread. You can refer to the example of Glsurfaceviewactivity. </span></p></p><p align="left"><p align="left">The Onmeasure () function is a bit tricky because this function is a critical part of the interaction between the component and the container, and<em>onmeasure ()</em> should be overloaded so that it can effectively and accurately represent the measured values of the part it contains. This is a bit more complicated, because we not only have to consider the limitations of the parent class (passed through <em>onmeasure ()</em> ), and we should know that once the width and height are measured, the <em>setmeasureddimension ()</em> will be called immediately. <span class="Apple-converted-space">Method. </span></p></p><p align="left"><p align="left">In summary, the Execute onmeasure () function is divided into several stages:</p></p><p align="left"><p align="left">1. The overloaded Onmeasure () method is called, and the height and width parameters are also involved (both Widthmeasurespec and heighmeasurespec Two are integer types), and you should consider the size limits of your product. Here you can refer to view.onmeasure (int, int)<span class="Apple-converted-space"> (the Connection content explains the entire measurement operation in detail). </span></p></p><p align="left"><p align="left">2. Your component should be onmeasure () to calculate the necessary measurement length and width to display your component, which should be consistent with the specification, although it can implement features other than specifications (in this case, the parent can choose what to do, including cutting, sliding, Commit an exception or call the Onmeasure () function again with a different parameter).</p></p><p align="left"><p align="left">3. Once the height and width have been calculated, you must call setmeasureddimension (int width, int height), Otherwise it will cause an Exception.</p></p><p align="left"><p align="left"><strong>Example of a custom component (a customized Component Example)</strong></p></p><p align="left"><p align="left">The <span class="Apple-converted-space">CustomView in API Demos<span class="Apple-converted-space"> provides an example of a custom component that is defined in the <span class="Apple-converted-space">LabelView<span class="Apple-converted-space"> class. </span></span></span></span></p></p><p align="left"><p align="left">The Labelview example involves all aspects of a custom component:</p></p><p align="left"><p align="left">· First let the custom component derive from the view class.</p></p><p align="left"><p align="left">· Write a constructor with parameters (parameters can originate from an XML file). Some of this processing has already been done in the view parent class, but some of the new parameters specific to the custom component used by Labelview need to be handled.</p></p><p align="left"><p align="left">· Some standard public functions, such as SetText (), settextsize (), settextcolor ()</p></p><p align="left"><p align="left">· Overloading the Onmeasure () method to determine the dimensions of the component (note: in labelview, it is implemented by a private function Measurewidth ())</p></p><p align="left"><p align="left">· The overloaded OnDraw () function displays the lable on the provided Canvas.</p></p><p align="left"><p align="left">In the example, you can see the use of custom component Labelview through Custom_view_1.xml. In the XML file, it is important to note that Android: and the app: two parameters of the mix, the app: parameters represent individuals in the application that are considered to be Labelview components, which are also defined as resources in the R class.</p></p><p align="left"><p align="left"><strong>Component Blending Technology Compound components (or Compound Controls)</strong></p></p><p align="left"><p align="left">If you do not want to create a fully customized component, but a new component produced by a combination of several existing components, then the hybrid component technology is more Appropriate. To put it simply, a couple of existing components can be packaged into a new component in a logical Combination. For example, a combo box component can be thought of as a mixture of a edittext and a button component with a pop-up List. If you click the button to select an item for the list,</p></p><p align="left"><p align="left">In android, there are other two view classes that can do similar effects:<span class="Apple-converted-space"> spinner and autocompletetextview, but combo box is easier to understand as an example. </span></p></p><p align="left"><p align="left">Here's A quick introduction to how to create a composite component:</p></p><p align="left"><p align="left">1. Typically starting with the layout class, create a derived class of the layout class. Perhaps in combo box we will choose the horizontal linearlayout as the parent class. Remember that other layout classes can be nested inside, so a hybrid component can be a mixture of any component. Note that, As with activity, you can either declare your component with an external XML file or nest it in your code.</p></p><p align="left"><p align="left">2. In the new hybrid Component's constructor, first Call all of the parent Class's constructors and pass in the corresponding Arguments. You can then set up some other aspects of your hybrid component, where to create the EditText component, and where to create the Popuplist Component. Note: You can also introduce some of your own attributes and parameters into the XML file, which can also be used by your hybrid components.</p></p><p align="left"><p align="left">3. You can also create a time listener to listen for events triggered by the view class in the new component, for example, to listen for a list option click event, you must update your EditText value after this time has Occurred.</p></p><p align="left"><p align="left">4. You may create some of your own properties, with access and modification methods. For example, allow the EditText initial value to be set and provide a way to access it.</p></p><p align="left"><p align="left">5. In the derived class of layout, you do not need to overload the OnDraw () and onmeasure () methods, because layout will have a better default handling. however, If you feel the need you can also reload it.</p></p><p align="left"><p align="left">6. You can also reload some on-series functions, such as overloads of OnKeyDown (), by pressing a key to select the corresponding value in the List.</p></p><p align="left"><p align="left">In summary, there are several advantages to the layout class as a base class:</p></p><p align="left"><p align="left">· As with activity, you can also declare your new component through an XML file, or you can nest it in your code.</p></p><p align="left"><p align="left">· The OnDraw () function and the onmeasure () function are not required to be overloaded, and two functions have been done Well.</p></p><p align="left"><p align="left">· You can quickly create your hybrid components, and you can use them as a single component.</p></p><p align="left"><p align="left"><strong>Examples of mixed components (Examples of Compound Controls)</strong></p></p><p align="left"><p align="left">In the Theapi Demos project in the API Demos project, There are two examples of List Classes--example 4 and Example6, The Speechview component is derived from the LinearLayout class to display the presentation function, The corresponding original code is List4.java and List6.java.</p></p><p align="left"><p align="left"><strong>Adjusting existing components (tweaking an Existing Component)</strong></p></p><p align="left"><p align="left">In some cases, you may have an easier way to create your Component. If you should have a very similar component, all you have to do is simply derive your component from this component and weigh in on some of the methods that need to be modified. By fully customizing the Component's approach you can do the same, but by generating new components with a flush view derivation, you can simply get some of the existing processing mechanisms that are probably what you want, and there is no need to start from Scratch.</p></p><p align="left"><p align="left">For example, There is an example of NotePad in the SDK (NotePad application<span class="Apple-converted-space"> ). This example demonstrates the practical details of many Android platforms, such as the one you'll learn to derive from EditView to automatically wrap a notepad. This is not a perfect example, because these APIs have changed a lot compared to earlier versions, but it does illustrate some of the problems. </span></p></p><p align="left"><p align="left">If you haven't seen the program yet, you can now import the Notepad routine in Eclipse (or simply view the source code by providing a link). In particular, view the<span class="Apple-converted-space"> definition of myedittext in Noteeditor.java. </span></p></p><p align="left"><p align="left">Here are a few things to keep in mind:</p></p><p align="left"><p align="left">1. <span class="Apple-converted-space"> <strong>statement (the Definition)</strong> </span></p></p><p align="left"><p align="left">This class is defined by the following line of Code:</p></p><p align="left"><p align="left">Publicstatic class Myedittext extends EditText</p></p><p align="left"><p align="left">o It is defined in the Noteeditor activity class, but it is common (public), so if necessary, it can be called from outside the Noteeditor by Noteeditor.myedittext.</p></p><p align="left"><p align="left">o It is a static class, which means that there is no "virtual-state method" that accesses data through the parent class, which makes the class A separate class that can rely on Noteeditor. For the creation of inline classes that do not need to be accessed from an external class, this is a clear way of thinking, guaranteeing that the resulting class is small and allows it to be easily invoked by other Classes.</p></p><p align="left"><p align="left">o It is an extension of the EditText class, and it is the parent class we have chosen to use from the Definition. When we are done, the new class can be used as a normal edittext.</p></p><p align="left"><p align="left">2. <span class="Apple-converted-space"> <strong>Initialization of classes</strong> </span></p></p><p align="left"><p align="left">In general, the parent class is called First. further, This is not a default constructor, but rather a constructor with Parameters. Because EditText is created using parameters extracted from an XML layout file, our constructors also take out parameters and pass them to the parent class.</p></p><p align="left"><p align="left">3. <span class="Apple-converted-space"> <strong>method overloading</strong> </span></p></p><p align="left"><p align="left">In this example, only one method of OnDraw () is Overloaded. But you can easily reload other required methods for your custom Components.</p></p><p align="left"><p align="left">For the Notepad example, by overloading the OnDraw () method we can draw blue lines on the Eidtview canvas (canvas class is passed by the overridden OnDraw () method). Call the Super.ondraw () function at the end of the Function. The method of the parent class should be called, but in this case we are calling after we have drawn the blue Line.</p></p><p align="left"><p align="left">4. <span class="Apple-converted-space"> <strong>using custom components</strong> </span></p></p><p align="left"><p align="left">now, we already have our own custom components, but how do we use them? In the Notepad example, a custom component is used directly in a predefined layout file, so let's take a look at the Note_editor.xml file in the Res/layout directory.</p></p><p align="left"><p align="left"><view xmlns:android= "http://schemas.android.com/apk/res/android"</p></p><p align="left"><p align="left">class= "com.android.notepad.noteeditor$myedittext"</p></p><p align="left"><p align="left">Id= "@+id/note"</p></p><p align="left"><p align="left">Android:layout_width= "fill_parent"</p></p><p align="left"><p align="left">android:layout_height= "fill_parent"</p></p><p align="left"><p align="left">android:background= "@android:d rawable/empty"</p></p><p align="left"><p align="left">android:padding= "10dip"</p></p><p align="left"><p align="left">android:scrollbars= "vertical"</p></p><p align="left"><p align="left">Android:fadingedge= "vertical"/></p></p><p align="left"><p align="left">o The custom component is created as a generic view class in XML and is described by a full path package. Note that inline classes are represented by noteeditor$myedittext, which is the standard way to reference inline classes in Java programming.</p></p><p align="left"><p align="left">o Other properties and parameters in the definition will be passed to the constructor of the custom component before uploading to the EditText constructor, so these parameters are also parameters that you use with the EditText Component. Note that you can also add your own parameters here, and we'll discuss the issue BELOW.</p></p><p align="left"><p align="left">That's all you need to do, admittedly this is a simple example. But the point is: how complex your requirements are, and how complex your custom components Are.</p></p><p align="left"><p align="left">A more complex component might need to reload more on series functions, and also require many unique functions to fully implement the functionality of the custom Component. The only limitation is your imagination and what you need to do with the Component.</p></p><p align="left"><p align="left"><strong>Let's start your modular journey NOW.</strong></p></p><p><p>As you can see, Android provides a sophisticated and powerful component model that allows you to do your job as much as Possible. From simple component sizing to component blending and even full customization of components, using these techniques flexibly, you should be able to get an Android program that perfectly matches your look requirements.</p></p><p><p>Android custom Controls (i) translation of official documents</p></p></span>

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.