Android mobile guard-custom composite widget component layout structure, android component

Source: Internet
Author: User

Android mobile guard-custom composite widget component layout structure, android component

Since the layout in the settings center entries is similar, you can consider using custom composite controls to simplify the implementation.

URL: http://www.cnblogs.com/wuyudong/p/5909043.html.

Custom composite controls

1. Extract the prepared layout file to a class for management. Next time you need to use this layout structure, directly use the objects corresponding to the composite control.

2. Extract the layout of the composite control to a separate xml file.

Create a layout file: setting_item_view.xml. Put the code in the layout file in the previous article

<? 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 = "wrap_content"> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: padding = "5dp"> <TextView android: 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"/> <TextView android: id = "@ + id/TV _des" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/TV _title" android: text = "auto update disabled" android: textColor = "#000" android: textSize = "18sp"/> <CheckBox android: id = "@ + id/cb_box" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: feature = "true" android: layout_centerVertical = "true"/> <View android: layout_width = "match_parent" android: layout_height = "1dp" android: layout_below = "@ id/TV _des" android: background = "#000"/> </RelativeLayout>

3. Use a separate class SettingItemView. java to load the layout file.

Package com. wuyudong. mobilesafe. view; import com. wuyudong. 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; public class SettingItemView extends RelativeLayout {private TextView TV _des; private CheckBox cb_box; public SettingItemView (Context context) {this (context, null);} public SettingItemView (Context context, AttributeSet attrs) {this (context, null, 0);} public SettingItemView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); // xml --> view converts the entries in the setting interface to the view object. inflate (context, R. layout. setting_item_view, this); // 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 the Custom Composite Control TextView TV _title = (TextView) findViewById (R. id. TV _title); TV _des = (TextView) findViewById (R. id. TV _des); cb_box = (CheckBox) findViewById (R. id. cb_box );}}

In this way, you only need a few lines of code to call the layout file.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView style = "@ style/TitleStyle" android: text = "set Center"/> <! -- <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: padding = "5dp"> <TextView android: id = "@ + id/TV _title" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "auto 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 = "auto update disabled" android: textColor = "#000" android: textSize = "18sp"/> <CheckBox android: id = "@ + id/cb_box" android: layout_alignParentRight = "true" android: layout_centerVertical = "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. settingItemView android: layout_width = "match_parent" android: layout_height = "wrap_content"> </com. wuyudong. mobilesafe. view. settingItemView> </LinearLayout>

After running the project, the following results are displayed:

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.