网友通过本文主要向大家介绍了手机360安全卫士,360安全卫士手机版,手机360安全卫士下载,安全卫士手机版,360手机安全卫士官网等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
手机安全卫士——在设置中心 自定义view和自定义属性,安全卫士view
自定义组合控件
1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView
2. 编写组合控件的布局文件,在自定义的View中加载
// 将自定义好的布局文件设置给当前的SettingItemView
View.inflate(getContext(), R.layout.view_setting_item, this);
3. 自定义属性
删除代码中对文本的动态设置,改为在布局文件中设置
在布局文件中增加新的命名空间
创建attrs.xml,定义相关属性
读取自定义的值,更新相关内容
activity_setting.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mobilesafe="http://schemas.android.com/apk/res/com.mxn.mobilesafe"//自定义命名空间。。。在布局文件中增加新的命名空间 android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="@style/TitleStyle" android:text="设置中心" /> <com.mxn.mobilesafe.view.SettingItemView android:id="@+id/siv_update" android:layout_width="match_parent" android:layout_height="wrap_content"
//自定义属性,不用android默认的属性
//从命名空间中找 mobilesafe:title="自动更新设置" mobilesafe:desc_on="自动更新已开启" mobilesafe:desc_off="自动更新已关闭" > </com.mxn.mobilesafe.view.SettingItemView> <com.mxn.mobilesafe.view.SettingItemView android:id="@+id/siv_address" android:layout_width="match_parent" android:layout_height="wrap_content" mobilesafe:title="归属地显示设置" mobilesafe:desc_on="归属地显示已开启" mobilesafe:desc_off="归属地显示已关闭" > </com.mxn.mobilesafe.view.SettingItemView> <com.mxn.mobilesafe.view.SettingClickView android:id="@+id/scv_address_style" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.mxn.mobilesafe.view.SettingClickView> <com.mxn.mobilesafe.view.SettingItemView android:id="@+id/siv_watchdog" android:layout_width="match_parent" android:layout_height="wrap_content" mobilesafe:title="看门狗设置" mobilesafe:desc_on="看门狗已开启" mobilesafe:desc_off="看门狗已关闭" > </com.mxn.mobilesafe.view.SettingItemView> </LinearLayout>
自定义属性 : attrs.xml。。创建attrs.xml,定义相关属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SettingItemView"> <attr name="title" format="string"></attr> <attr name="desc_on" format="string"></attr> <attr name="desc_off" format="string"></attr> </declare-styleable> </resources>
SettingItemView.java
/* * 设置中心的自定义控件,自定义View */ public class SettingItemView extends RelativeLayout { TextView tvTitle; TextView tvDesc; CheckBox cbStatus; private String mtitle; private String mdescon; private String mdescoff;
String namespace = "http://schemas.android.com/apk/res/com.mxn.mobilesafe";//命名空间 public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); // TODO Auto-generated constructor stub initView(); } public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub initView(); // int attributeCount = attrs.getAttributeCount(); // for(int i=0;i<attributeCount;i++){ // String attrsname = attrs.getAttributeName(i); // String attrvalue = attrs.getAttributeValue(i); // System.out.println(attrsname+"="+attrvalue); // // } } public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub
// 读取自定义的值,更新相关内容 //根据属性名称获取属性的值 mtitle = attrs.getAttributeValue(namespace , "title"); mdescon = attrs.getAttributeValue(namespace , "desc_on"); mdescoff = attrs.getAttributeValue(namespace , "