在前面的文章中,已经实现了“设置中心”第一栏的功能以及布局

本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,转载请注明出处。

自定义属性声明

接下来实现其他栏的布局和功能,由于它们之间的功能和布局类似,只是属性名称不同。所以本文在自定义控件的基础上实现自定义属性

首先参考标准控件的源码,这里选择TextView

源码路径为:D:\adt-bundle-windows-x86_64_20140101\sdk\platforms\android-18\data\res\values

打开本文件夹下的attrs.xml文件,找到下面的代码:

    <declare-styleable name="TextView">
<!-- Determines the minimum type that getText() will return.
The default is "normal".
Note that EditText and LogTextBox always return Editable,
even if you specify something less powerful here. -->
<attr name="bufferType">
<!-- Can return any CharSequence, possibly a
Spanned one if the source text was Spanned. -->
<enum name="normal" value="0" />
<!-- Can only return Spannable. -->
<enum name="spannable" value="1" />
<!-- Can only return Spannable and Editable. -->
<enum name="editable" value="2" />
</attr>
<!-- Text to display. -->
<attr name="text" format="string" localization="suggested" />
<!-- Hint text to display when the text is empty. -->
<attr name="hint" format="string" />

于是我们也可以模仿关键节点,写出自定义属性,工程res\values文件夹下新建attrs.xml文件,添加代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.wuyudong.mobilesafe.view.SettingItemView">
<attr name="destitle" format="string"/>
<attr name="desoff" format="string"/>
<attr name="deson" format="string"/>
</declare-styleable>
</resources>

构造方法中获取自定义属性值

接下来定义命名空间,也是参考android标准来写

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
……

mobilesafe替换掉原有android

com.wuyudong.mobilesafe必须这样编写,替换掉了android,代表当前应用自定义属性
xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"

修改后的代码如下:

    <com.wuyudong.mobilesafe.view.SettingItemView
xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
mobilesafe:destitle="自动更新设置"
mobilesafe:desoff="自动更新已关闭"
mobilesafe:deson="自动更新已开启" >
</com.wuyudong.mobilesafe.view.SettingItemView>

自定义属性值已经搞定,现在是SettingItemView类如何获取这些值?

在SettingItemView类的构造函数中调用initAttrs函数,然后通过initAttrs函数实现属性的返回。先通过一些小的实践来熟悉相关的API

   /**
* 返回属性集合中自定义属性的属性值
* @param attrs 构造方法中维护好的属性集合
*/
private void initAttrs(AttributeSet attrs) {
//获取属性的总个数
Log.i(tag,"attrs.getAttributeCount(): " + attrs.getAttributeCount());
//获取属性名称以及属性值
for (int i = 0; i < attrs.getAttributeCount(); i++) {
Log.i(tag, "name = " + attrs.getAttributeName(i));
Log.i(tag, "value = " + attrs.getAttributeValue(i));
Log.i(tag, "==================分割线======================");
}
}

运行项目后在Logcat中打印下面的日志信息:

解释一下上面的部分代码:

value = @2131427413对应的十六进制值为:7F0B0055,其实就是对应的R文件中

public static final int siv_update=0x7f0b0055;

其他的都很简单

接着我们使用其他的API来进行获取属性的值

private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.wuyudong.mobilesafe";
........... /**
* 返回属性集合中自定义属性的属性值
* @param attrs 构造方法中维护好的属性集合
*/
private void initAttrs(AttributeSet attrs) {
/* //获取属性的总个数
Log.i(tag,"attrs.getAttributeCount(): "+attrs.getAttributeCount());
//获取属性名称以及属性值
for (int i = 0; i < attrs.getAttributeCount(); i++) {
Log.i(tag, "name = " + attrs.getAttributeName(i));
Log.i(tag, "value = " + attrs.getAttributeValue(i));
Log.i(tag, "==================分割线======================");
}*/
String destitle = attrs.getAttributeValue(NAMESPACE, "destitle");
String desoff = attrs.getAttributeValue(NAMESPACE, "desoff");
String deson = attrs.getAttributeValue(NAMESPACE, "deson");
Log.i(tag, destitle);
Log.i(tag, desoff);
Log.i(tag, deson);
}

运行项目后在Logcat中打印下面的日志信息:

说明已经成功获取所设置的属性值

这样就可以复用代码实现第二栏的电话归属地的布局

    <com.wuyudong.mobilesafe.view.SettingItemView
xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
mobilesafe:destitle="自动更新设置"
mobilesafe:desoff="自动更新已关闭"
mobilesafe:deson="自动更新已开启" >
</com.wuyudong.mobilesafe.view.SettingItemView> <com.wuyudong.mobilesafe.view.SettingItemView
xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
mobilesafe:destitle="电话归属地的显示设置"
mobilesafe:desoff="归属地的显示已关闭"
mobilesafe:deson="归属地的显示已开启" >
</com.wuyudong.mobilesafe.view.SettingItemView>

Android 手机卫士--自定义属性的更多相关文章

  1. Android 手机卫士--参照文档编写选择器

    本文来实现<Android 手机卫士--导航界面1的布局编写>中的图片选择器部分的代码. 本文地址:http://www.cnblogs.com/wuyudong/p/5944356.ht ...

  2. Android 手机卫士--设置界面&功能列表界面跳转逻辑处理

    在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...

  3. Android 手机卫士--确认密码对话框编写

    本文接着实现“确认密码”功能,也即是用户以前设置过密码,现在只需要输入确认密码 本文地址:http://www.cnblogs.com/wuyudong/p/5940718.html,转载请注明出处. ...

  4. Android 手机卫士--签名文件说明&包名说明

    在<Android 手机卫士--打包生成apk维护到服务器>一文中,实现了新版本的apk到服务器,当打开客户端apk的时候,发现有新版本,提示更新.还实现了利用xutils工具实现了从服务 ...

  5. Android 手机卫士--弹出对话框

    在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...

  6. android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码

    Android精选源码 android身份证.银行卡号扫描源码 android仿bilibili客户端 android一款3D 指南针 源码 android手机卫士app源码 android提醒应用, ...

  7. Android 手机卫士--阶段小结1

    本文地址:http://www.cnblogs.com/wuyudong/p/5904528.html,转载请注明源地址. 本文对之前手机卫士开发进行一个小结. 1.SplashActivity 版本 ...

  8. Android 手机卫士--安装过程中点击回退按钮

    本文地址:http://www.cnblogs.com/wuyudong/p/5903707.html,转载请注明源地址. 在手机卫士之前的版本升级的对话框中: 有的用户暂时不想更新,没有点击“稍后再 ...

  9. Android 手机卫士--xutils说明与下载方法使用

    xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...

随机推荐

  1. CSS中div覆盖另一个div

    将一个div覆盖在另一个div上有两种手段:一是设置margin为负值,二是设置绝对定位. 可以根个人情况设置z-index的值 1->position 为absolute的情况 <htm ...

  2. WebAPi返回类型到底应该是什么才合适,这是个问题?

    前言 有些问题只有真正遇到或者用到并且多加思考才会想到,平常若作为自学的心态去学习则不会考虑太多,我慢慢明白对于那些有太多要学的东西或者说的更加明确而且具体一点的话,如果对于你现在不是迫切要学或者需要 ...

  3. MySQL学习笔记九:存储过程,存储函数,触发器

    存储过程 1.存储过程由一组特定功能的SQL语句组成,对于大型应用程序优势较大,相对不使用存储过程,具有以下优点: a.性能提高,因为存储过程是预编译的,只需编译一次,以后调用就不须再编译 b.重用性 ...

  4. [Python] python vs cplusplus

    一些学习过程中的总结的两种语言的小对比,帮助理解OO programming. Continue... 字典 序列 --> 字典 Python: def get_counts(sequence) ...

  5. js ES6 对字符的操作注意事项

    1.codePointAt方法是测试一个字符由两个字节还是由四个字节组成的最简单方法. function is32Bit(c) { return c.codePointAt(0) > 0xFFF ...

  6. 关于BFC不会被浮动元素遮盖的一些解释

    简介 在清除浮动一文中提到BFC不会被浮动元素遮盖,并没有详细探究表现行为.规范中指出,在同一个BFC内,作为子元素的BFC的border-box不应该覆盖同为子元素的浮动元素的margin-box. ...

  7. WCF局域网内使用代理无法访问解决方法

    问题描述 在大部分事业单位上网都是需要使用代理的,前几天带着一个同事写的程序过来部署,部署以后各个客户端通过WCF相互通讯,那么其中一个地方在本地局域网测试是没有问题的. 后发现一部分是原因是由于代理 ...

  8. mybatis入门基础(三)----SqlMapConfig.xml全局配置文件解析

    一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) settings(全局配置参数) typeAiases(类型别名) typeHandlers(类型处理器 ...

  9. entity framework 删除数据库出现错误的解决方法--最土但是很有效的方法

    无法删除数据库,因为该数据库当前正在使用. public ChinaerContext() : base("name=ContextConn") { // Database.Set ...

  10. 关于log4net日志的配置流程

    最近又重新整理一下log4net日志的配置,现在记录一下流程和一些遇到的问题,以备后续使用,具体的配置参数等信息.此文无,见谅! 1. 下载log4net.dll文件(网上很多,随便找一个!) 2. ...