Because the layout in the Settings center entry is similar, consider using a custom composite control to simplify the implementation
This article address: http://www.cnblogs.com/wuyudong/p/5909043.html, reprint please indicate source address.
Customizing a composite Control
1. The layout files that have been prepared are extracted into a class to be managed, and the next time you need to use this layout structure, you use the corresponding object of the composite control directly.
2. Extract the layout of the combined control into a single XML
New layout file: Setting_item_view.xml, put the code in the layout file in the previous article
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:padding= "5DP" > <TextViewAndroid:id= "@+id/tv_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Automatic Update settings"Android:textcolor= "#000"android:textsize= "18SP" /> <TextViewAndroid:id= "@+id/tv_des"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/tv_title"Android:text= "Automatic Update is OFF"Android:textcolor= "#000"android:textsize= "18SP" /> <CheckBoxAndroid:id= "@+id/cb_box"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centervertical= "true" /> <ViewAndroid:layout_width= "Match_parent"Android:layout_height= "1DP"Android:layout_below= "@id/tv_des"Android:background= "#000" /> </Relativelayout></Relativelayout>
3. Use a separate class Settingitemview.java to load this section of the layout file.
PackageCom.wuyudong.mobilesafe.view;ImportCOM.WUYUDONG.MOBILESAFE.R;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;Importandroid.widget.RelativeLayout;ImportAndroid.widget.TextView; Public classSettingitemviewextendsRelativelayout {PrivateTextView tv_des; PrivateCheckBox Cb_box; PublicSettingitemview (Context context) { This(Context,NULL); } PublicSettingitemview (Context context, AttributeSet attrs) { This(Context,NULL, 0); } PublicSettingitemview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //Xml-->view Converting an entry for the settings interface into a View objectView.inflate (context, R.layout.setting_item_view, This); //is equivalent to the following two lines of code /** View view = view.inflate (context, r.layout.setting_item_view, null); * This.addview (view); */ //Title description in custom composite controlsTextView Tv_title =(TextView) Findviewbyid (r.id.tv_title); Tv_des=(TextView) Findviewbyid (r.id.tv_des); Cb_box=(CheckBox) Findviewbyid (R.id.cb_box); }}
This will only require a few simple lines of code to complete the layout file call
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewstyle= "@style/titlestyle"Android:text= "Set Center" /> <!-- <relativelayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Andro id:padding= "5DP" > <textview android:id= "@+id/tv_title" android:layout_width= "Wrap_cont Ent "android:layout_height=" wrap_content "android:text=" Automatic Update Settings "android:textcolor=" #000 " Android:textsize= "18sp"/> <textview android:id= "@+id/tv_des" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/tv_title" android:text= "Automatic Update is Off" android:textcolor= "#000" android:textsize= "18sp"/> <check Box android:id= "@+id/cb_box" android:layout_alignparentright= "true" Android:layout_center Vertical= "true" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <view Android:layout_below= "@id/tv_des" android:background= "#000" android:layout_width= "Match_parent" android:layout_height= "1DP"/> </RelativeLayout> - <Com.wuyudong.mobilesafe.view.SettingItemViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </Com.wuyudong.mobilesafe.view.SettingItemView></LinearLayout>
After you run the project, you have the following effects:
Android Phone Defender--custom combo control widget layout structure