Recommended reading:Brief analysis Android phone guard turns off automatic update
The custom combination control completed in the previous section is not flexible enough. In terms of the display information of the control, you can customize your own properties according to the system properties.
In the previous section, there are three controls in the SettingItemView, namely the TextView title, TextView description, and CheckBox checkbox
Custom attributes tsh:title="large title" and tsh:desc_on="subtitle on", tsh:desc_off="subtitle off"
Add namespace, xmlns:tsh="/apk/res/package name"
Create a file in the res/values/ directory
Add node <declare-styleable name=”TextView”>
Add node under node <attr name="title" format="string"/>, and add nodes with other two attributes
When using the layout file, a constructor with two parameters will be called
In this constructor, an AttributeSet object will be passed
Call the getAttributeValue() method of the AttributeSet object to get the attribute value, parameter: index position, not recommended
Call the getAttributeValue(namespace,name) method of the AttributeSet object, parameters: namespace, attribute name
Call the setText() method of the TextView object and set it directly
The description part, in the setChecked() method, judge and then set
package ; import ; import ; import ; import ; import ; import ; import ; public class SettingItemView extends RelativeLayout { private TextView tv_title; private TextView tv_desc; private CheckBox cb_status; private String desc_on; private String desc_off; /** * Initialize the View object * @param context */ private void initView(Context context) { (context, .setting_item_view, this); cb_status=(CheckBox) (.cb_status); tv_desc=(TextView) (.tv_desc); tv_title=(TextView) (.tv_title); } /** * Determine whether to select * @return */ public boolean isChecked(){ return cb_status.isChecked(); } /** * Set whether to select * @param status */ public void setChecked(boolean status){ if(status){ tv_desc.setText(desc_on); }else{ tv_desc.setText(desc_off); } cb_status.setChecked(status); } /** * Set display text * @param text */ public void setDesc(String text){ tv_desc.setText(text); } public SettingItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); //Get the passed attributeString title=("/apk/res/", "title"); tv_title.setText(title); desc_on=("/apk/res/", "desc_on"); desc_off=("/apk/res/", "desc_off"); } public SettingItemView(Context context) { super(context); initView(context); } }
activity_setting.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:tsh="/apk/res/" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ccc" android:gravity="center" android:text="Settings Center" android:textSize="20sp" /> < tsh:title="Set updating automatically" tsh:desc_on="Setting automatic updates to enable" tsh:desc_off="Setting automatic updates to close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:> </> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TextView"> <attr name="title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable> </resources>
The above is a related introduction to the properties of Android mobile phone guard custom controls. I hope it will be helpful to everyone!