Android: Creating a custom control

Source: Internet
Author: User
<span id="Label3"></p><p><p>In the first two sections we have learned about some of the common controls in Android and the usage of basic layouts, but we didn't pay attention to the inheritance structure of these controls and layouts, and now it's time to look at 3.26.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p align="center"><p align="center">Figure 3.26</p></p><p><p></p></p><p><p>As you can see, all the controls we use are inherited directly or indirectly from View, and all of the layouts we use are directly or indirectly inherited from Viewgroup. View is one of the most basic UI components on Android that can draw a rectangular area on the screen and respond to various events in this area, so the various controls we use actually add their own functionality to the View Base. ViewGroup is a special view that can contain many sub-view and sub-viewgroup, and is a container for placing controls and Layouts.</p></p><p><p>At this point we can think about how we could use the inheritance structure above to create a custom control if the system comes with a control that doesn't meet our NEEDS. The answer is yes, let's learn about two simple ways to create a custom Control. Prepare your work and create a uicustomviews project First.</p></p><p><p></p></p><p><p>3.4.1 Introducing Layouts</p></p><p><p></p></p><p><p>If You've ever used an iphone you should know that almost every iphone app has a title bar on the top of the screen, with one or two buttons on the title bar for return or other actions (iPhone has no entity return key). Many Android apps now like to mimic the IPhone's style by placing a title bar at the top of the Interface. Although the Android system has provided the title bar function for each activity, here we still decide not to use it, but instead create a custom title Bar.</p></p><p><p>After two previous sections of learning, I would like to create a title bar layout is not a difficult thing for you, just need to add two Button and a TextView, and then placed in the layout is Ok. however, there is a problem, in general, there may be many activities in our program need such a title bar, if you write in each live layout of the same title bar code, it will obviously lead to a lot of code Duplication. This time, we can</p></p><p><p>To solve this problem by using the introduction layout, Create a new layout title.xml, as shown in the following code:</p></p><p><p></p></p><p><p><strong><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/android"</strong> <strong>android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "@drawable/title _BG "></strong></p></p><p><p></p></p><p><p><strong><buttonandroid:id= "@+id/title_back" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_margin= "5dip" android:background= "@drawable/back_bg" android:text= "back" android:textcolor= "#fff"/></strong></p></p><p><p></p></p><p><p><strong><textviewandroid:id= "@+id/title_text" android:layout_width= "0dip" android:layout_height= "wrap_content" Android : layout_gravity= "center" android:layout_weight= "1" android:gravity= "center" android:text= "Title text" android: textcolor= "#fff" android:textsize= "24sp"/></strong></p></p><p><p></p></p><p><p><strong><buttonandroid:id= "@+id/title_edit" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_margin= "5dip" android:background= "@drawable/edit_bg" android:text= "Edit" android:textcolor= "#fff"/></strong></p></p><p><p></p></p><p><p><strong></LinearLayout></strong></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>As you can see, We've added two buttons and one TextView to the linearlayout, and the button on the left</p></p><p><p>Can be used to return, the Button on the right can be used for editing, the middle TextView can display a section of the title Text. Most of the attributes in the above code are already seen, and i'll show you a couple of properties that haven't been mentioned Before. Android:background is used to assign a background to a layout or control, you can fill it with a color or a picture, I've prepared three pictures in advance, title_bg.png, back_bg.png, and edit_bg.png, respectively, for the title bar, Returns the background of the button and edit Button. In addition, we used the Android:layout_margin property in two buttons, which can specify how far the control will be offset in the upper and lower left and right directions, but can also use Android:layout_marginleft or android: Properties such as Layout_margintop to specify the distance that the control is offset in a direction individually.</p></p><p><p>Now that the title bar layout has been written, the rest is how to use the title bar in the program, modify</p></p><p><p>The code in Activity_main.xml is as Follows:</p></p><p><p></p></p><p><p><strong><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/android"</strong> <strong>android:layout_width= "match_parent" android:layout_height= "match_parent" ></strong></p></p><p><p></p></p><p><p><strong><include layout= "@layout/title"/></strong></p></p><p><p></p></p><p><p></p></p><p><p><strong></LinearLayout></strong></p></p><p><p>That's right! We just need to introduce the title bar layout through a line of include Statements. finally, don't forget to hide the System's own title bar in mainactivity, as shown in the following code:</p></p><p><p></p></p><p><p><strong>public class Mainactivity extends Activity {</strong></p></p><p><p></p></p><p><p></p></p><p><p><strong>@Override</strong></p></p><p><p><strong>Protected voidoncreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); requestwindowfeature ( window.feature_no_title);</strong></p></p><p><p><strong>Setcontentview (r.layout.activity_main);</strong></p></p><p><p><strong>}</strong></p></p><p><p></p></p><p><p></p></p><p><p><strong>}</strong></p></p><p><p>Now run the program, as shown in effect 3.27.</p></p><p><p></p></p><p><p></p></p><p align="center"><p align="center">Figure 3.27</p></p><p><p>In this way, no matter how many layouts you need to add a title bar, just one line of the include statement is Available.</p></p><p><p></p></p><p><p>3.4.2 Creating a custom control</p></p><p><p></p></p><p><p>The technique of introducing layouts does solve the problem of repeating layout code, But if there are controls in the layout that require the ability to respond to events, we still need to write the event registration code separately for each of these controls in each Activity. For example, the return button in the title bar, in fact, regardless of which activity, the function of the button is the same, that is, destroy the current Activity. If you need to re-register the click event of the Return button in each activity, there is no doubt that there is a lot of duplicate code added, which is best solved by using a custom Control.</p></p><p><p>The new titlelayout inherits from linearlayout, making it our custom title bar control, as shown in the following code:</p></p><p><p></p></p><p><p><strong>public class Titlelayout extends LinearLayout {</strong></p></p><p><p></p></p><p><p></p></p><p><p><strong>Public titlelayout (context context, attributeset attrs) {</strong></p></p><p><p><strong>Super (context, attrs);</strong></p></p><p><p><strong>Layoutinflater.from (context). Inflate (r.layout.title, this);</strong></p></p><p><p><strong>}</strong></p></p><p><p></p></p><p><p><strong>}</strong></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>First we rewrite the constructors with two parameters in linearlayout, and introduce titlelayout in the Layout.</p></p><p><p>The constructor is called by the Control. The title bar layout needs to be dynamically loaded in the constructor, which is accomplished with the help of Layoutinflater. The Layoutinflater from () method allows you to build a Layoutinflater object and then call the inflate () method to dynamically load a layout file, and the inflate () method receives two parameters, the first One parameter is the ID of the layout file to be loaded, here we pass in r.layout.title, the second parameter is to add a parent layout to the loaded layout, where we want to specify as titlelayout, and then pass in this directly.</p></p><p><p>Now that the custom control has been created, we need to add this custom control to the layout file and modify</p></p><p><p>The code in Activity_main.xml is as Follows:</p></p><p><p></p></p><p><p><strong><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/android"</strong> <strong>android:layout_width= "match_parent" android:layout_height= "match_parent" ></strong></p></p><p><p></p></p><p><p><strong><com.example.uicustomviews.titlelayoutandroid:layout_width= "match_parent" android:layout_height= "WRAP_ Content</strong></p></p><p><p><strong>></com.example.uicustomviews.TitleLayout></strong></p></p><p><p></p></p><p><p></p></p><p><p><strong></LinearLayout></strong></p></p><p><p>The way to add custom controls and add normal controls is basically the same, except that when you add a custom control, we need to indicate the full class name of the control, and the package name cannot be omitted Here.</p></p><p><p>Rerun the program, and you'll find that the effect is the same as using the introduction Layout. Then we try to register the click event for the button in the title bar and modify the code in Titlelayout as Follows:</p></p><p><p></p></p><p><p><strong>public class Titlelayout extends LinearLayout {</strong></p></p><p><p></p></p><p><p></p></p><p><p><strong>Public titlelayout (contextcontext, attributeset attrs) {super (context, attrs); Layoutinflater.from (context). Inflate (r.layout.title,this); Button Titleback = (button) Findviewbyid (r.id.title_back); Buttontitleedit = (Button) Findviewbyid (r.id.title_edit); titleback.setonclicklistener (new onclicklistener () {</strong></p></p><p><p><strong>@Override</strong></p></p><p><p><strong>public void OnClick (View v) {(Activity) getcontext ()). finish ();</strong></p></p><p><p><strong>}</strong></p></p><p><p><strong>});</strong></p></p><p><p><strong>Titleedit.setonclicklistener (new Onclicklistener () {</strong></p></p><p><p><strong>@Override</strong></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>public void OnClick (View V) {</strong></p></p><p><p><strong>Toast.maketext (getcontext (), "you clicked editbutton",</strong></p></p><p><p><strong>Toast.length_short). Show ();</strong></p></p><p><p><strong>}</strong></p></p><p><p><strong>});</strong></p></p><p><p><strong>}</strong></p></p><p><p></p></p><p><p><strong>}</strong></p></p><p><p>first, the Findviewbyid () method to get the instance of the button, and then call the Setonclicklistener () method to register the click event for two buttons, when the return button click to destroy the current activity, when clicked on the Edit button to pop up a text. Rerun the program and click on the edit button, as shown in effect 3.28.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p align="center"><p align="center">Figure 3.28</p></p><p><p></p></p><p><p>That way, whenever we introduce titlelayout in a layout, the click events of the Return button and edit button are automatically implemented, and there is a lot of work to write repetitive code.</p></p><p><p>Android: Creating a custom control</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.