Android instance-Mobile security Defender (vii)-Custom combo controls

Source: Internet
Author: User

First, the goal.

Combines multiple system controls (TextView, buttons, checkboxes, and so on) into a single custom control and is used like a system control. The 1th Automatic Update control is based on a relative layout, and the 2nd Automatic Update control is a custom composite control that can be reused like a system control such as a general textview.

Custom controls

Second, the code implementation.

1. Create a new XML file (named Model_setting_item.xml) under the Layout folder to save the layout of the custom control.

2. Organize individual system controls in a new XML file (Model_setting_item.xml) according to design. Here you can copy the code from the relative layout in the layout file (Activity_setting.xml) of the sixth lecture to the new XML file, preserving the relative layout header in the new XML file when the primary copy is in place.

New XML file (model_setting_item.xml) code:

1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Wrap_content"4android:layout_height= "Wrap_content"5android:layout_marginleft= "10dip"6android:layout_marginright= "10dip" >7 8<TextView9Android:id= "@+id/setting_update_title"TenAndroid:layout_width= "Wrap_content" Oneandroid:layout_height= "Wrap_content" Aandroid:layout_margintop= "10dip" -android:text= "Automatic Updates" -Android:textcolor= "#000000" theAndroid:textsize= "22dip"/> -  -<TextView -Android:id= "@+id/setting_update_content" +Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" +android:layout_below= "@id/setting_update_title" AAndroid:text= "Allow Automatic Updates when software is networked" atAndroid:textcolor= "#88000000" -Android:textsize= "18dip"/> -  -<CheckBox -Android:id= "@+id/setting_update_checkbox" -Android:layout_width= "Wrap_content" inandroid:layout_height= "Wrap_content" -Android:layout_alignparentright= "true" toAndroid:layout_centervertical= "true"/> +  -<View theAndroid:layout_width= "Match_parent" *android:layout_height= "0.1dip" $android:layout_below= "@id/setting_update_content"Panax Notoginsengandroid:layout_margintop= "2dip" -android:background= "#70000000"/> the  +</RelativeLayout>
View Code

3. The way to create a marquee control (see fourth) creates a new class that inherits the relative layout (named Settingitemview) under the Custom tool class package (Com.example.mobilesafe.ui). In the new Class (Settingitemview) code:

①. Define three construction methods (same as "marquee" section): Menu bar Source--generate Constructors from superclass, click OK in the popup dialog box.

The code is as follows:

1  Public classSettingitemviewextendsRelativelayout {2 3      PublicSettingitemview (context context, AttributeSet attrs,intDefstyle) {4         Super(context, attrs, defstyle);5     }6 7      PublicSettingitemview (Context context, AttributeSet attrs) {8         Super(context, attrs);9     }Ten  One      PublicSettingitemview (Context context) { A         Super(context); -     } -}
View Code

②. Create a method that initializes the layout file (named Iniview (context context)), passing a context as a parameter to the method. In method Iniview (context context) through the View object's inflate (context context, int resource, ViewGroup root) The resource method converts a resource file (the layout file) into a View object and loads it into the ViewGroup, with the parameter context contexts being a contextual,int resource is the resource file that needs to be converted (This is the newly created XML file (Model_setting_item.xml), ViewGroup root is the parent component that needs to be loaded (this is settingitemview.this).

Iniview Method Code (note the COM.EXAMPLE.MOBILESAFE.R resource bundle when you import the R package):

1 Private void Iniview (Context context) {2         View.inflate (context, R.layout.model_setting_item, Settingitemview.  this); 3     }
View Code

③. A method Iniview (context) that initializes the layout file in three constructor methods.

4. Use a custom combo control.

    In the layout file (here or in the Settings center layout file Activity_setting.xml as an example) to add a custom composite control, which is defined in the The relative layout in the Activity_setting.xml file creates a control with the full path name of the custom control, setting properties such as width, height, and ID.

The code is as follows:

1  <Com.example.mobilesafe.ui.SettingItemView2         android:id= "@+id/setting_update"3          android:layout_width= "Wrap_content"4         android:layout_height= "Wrap_content" >5     </com.example.mobilesafe.ui.SettingItemView>
View Code

Android instance-Mobile security Defender (vii)-Custom combo controls

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.