The Android system comes with a lot of components, but it certainly meets our individual needs, so we need to customize the Android UI components to achieve our individual needs for ease of development.
To customize a composite control:
1, customize a view, you need to inherit the relative layout, linear layout, such as ViewGroup subclasses. ViewGroup is a container for other controls that can be put in various components.
2, the implementation of the parent class 3 construction methods. In general, the initial custom layout file needs to be initialized in the constructor method.
One parameter construction method: For the new control, use the
Two parameter creation method: In the call layout file to use
Two parameter creation method: Pass a layout file with style
3, according to the requirements, define some API methods.
4. Customize the properties of the control as needed. You can refer to the TextView property write.
5. Custom namespaces. xmlns:xxx= "http://schemas.android.com/apk/res/< package name >" XXX for Scheam name
6. Customize our properties, define properties in Res/values/attrs.xml (Create property file)
Similar:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "TextView" >
<!--properties of the custom control--
<attr name= "desc_on" format= "string"/>
<attr name= "Desc_off" format= "string"/>
<attr name= "Titles" format= "string"/>
</declare-styleable>
</resources>
7. Use a custom property.
For example:
andy:desc_off= "Set Automatic Update is off"
Andy:desc_on= "Set up Automatic Updates is turned on"
Andy:titles= "Set up Automatic Updates"
8, in our custom control with two parameters of the construction method, AttributeSet attrs Remove the custom property value, associated to the custom layout file corresponding to the control.
Code examples are as follows;
1 Defining a Custom control: Setting_item_view.xml layout file
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" 68dip "> <textview android:id=" @ +id/tv_title "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout _marginleft= "10dip" android:layout_margintop= "8dip" android:text= settings automatically update "android:textcolor=" #00000 0 "android:textsize=" 20sp "/> <textview android:id=" @+id/tv_desc "android:layout_width=" wrap _content "android:layout_height=" wrap_content "android:layout_below=" @id/tv_title "Android:layout_mar ginleft= "10dip" android:text= "Automatic Update has been turned off" android:textcolor= "#88000000" android:textsize= "18sp"/> <checkbox android:id= "@+id/cb_status" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layOut_alignparentright= "true" android:layout_centervertical= "true" android:layout_marginright= "10dip"/> <view android:clickable= "false" Android:layout_width= "Match_parent" android:layout_height= "0.2dip "Android:layout_alignparentbottom=" true "android:layout_marginleft=" 5dip "android:layout_marginright = "5dip" android:background= "#000000"/></relativelayout>
2 called in the corresponding activity layout file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:andy=" Http://schemas.android.com/apk/res/com.andy.mobilesafe " android:layout_width=" Match_parent " android:layout_height=" match_parent " android:orientation=" vertical "> < Com.andy.mobilesafe.ui.SettingItemView android:id= "@+id/siv_update" android:layout_width= "wrap_content " android:layout_height=" wrap_content " andy:desc_off=" setting Automatic Update has been turned off " andy:desc_on=" setting Automatic Update is turned on " Andy:titles= "Set up Automatic Updates"/></linearlayout>
3 Custom properties: Res/values/attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "TextView" > <!--properties of custom Controls-- <attr name= "desc_on" format= "string"/> <attr name= "Desc_off" format= "string "/> <attr name=" titles "format=" string "/> </declare-styleable></resources>
4 Implement custom components, inherit ViewGroup subclasses, implement construction methods, and corresponding API methods
Package Com.andy.mobilesafe.ui;import Com.andy.mobilesafe.r;import Android.content.context;import Android.util.attributeset;import Android.view.view;import Android.widget.checkbox;import Android.widget.relativelayout;import android.widget.textview;/** * @author zhang,tianyou * @version November 15, 2014 PM 10 : 22:50 * * Custom Combo Control two x textview a checkbox a view */public class Settingitemview extends Relativelayout {private C Heckbox cb_status;private TextView tv_title;private TextView tv_desc;private string Desc_on;private string desc_off;/** * Initialize Layout file * * @param context */private void Initview (Context context) {//second for layout file root third parameter for layout file parent class//Put a layout file View and load in SE Ttingitemviewview.inflate (context, R.layout.setting_item_view, this);//view already loaded the Settingitemviewcb_status = ( CheckBox) This.findviewbyid (r.id.cb_status), Tv_desc = (TextView) This.findviewbyid (R.ID.TV_DESC); tv_title = ( TextView) This.findviewbyid (r.id.title);} Public Settingitemview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//This is a style call that can be passed initview (context);} /** * With two parameters of the construction method, the layout file using the time call * * @param context * @param attrs * Get the attribute value */public Settingitemview (Context cont Ext, AttributeSet attrs) {Super (context, attrs);//Custom layout Use the constructor method of the call Attrs for the configuration of the property layout file using Initview (context); String titles = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.andy.mobilesafe", "titles");d ESC_ Off = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.andy.mobilesafe", "Desc_off");d esc_on = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.andy.mobilesafe", "desc_on"); Tv_title.settext ( titles); Setdesc (Desc_off);} Public Settingitemview (Context context) {super (context);//New Time Use Initview (context);} /** * Check if the combo control has a check * * @return */public boolean isChecked () {return cb_status.ischecked ();} /** * Set the combo control to select * * @param checked */public void setchecked (Boolean checked) {if (checked) {Setdesc (desc_on);} else {setdesc (desc_off);} cb_status.setchecked (checked);} /** * Sets the description information for the combined control * * @param text */public void Setdesc (String text) {tv_desc.settext (text);}}
Custom UI components and properties for Android development