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

本文地址: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>

最新文章

  1. &lt;!DOCTYPE&gt;标签的定义与用法
  2. Android root + 修改host
  3. 16.语句include和require的区别是什么?为避免多次包含同一文件,可用(?)语句代替它们?
  4. 【VB6】vbRichClient5.cWebServer实现一个简单web服务器
  5. 一些有用的UtilityExtend小方法
  6. Windows Live Writer配置
  7. (转载)前端构建工具gulp使用
  8. JDK安装(windows/linux)
  9. 不知还有人遇到这个问题没有:数据库 &#39;xxx&#39; 的版本为 706,无法打开。此服务器支持 661 版及更低版本。不支持降级路径。
  10. 视酷即时通讯系统应用源码 V1.0
  11. QQ群开放接口
  12. java.lang.NoSuchFieldError: deferredExpression解决
  13. 【源代码】基于Android和蓝牙的单片机温度採集系统
  14. mysql设置root的密码
  15. Python Requests 库学习笔记
  16. MongoDB入门系列:复制机制
  17. python中读取文件数据时要注意文件路径
  18. 微信ChatEmoji表情适配,对微信公众号开发有帮助
  19. Kotlin入门(28)Application单例化
  20. codeblock用法

热门文章

  1. 爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
  2. NFS Volume Provider(Part III) - 每天5分钟玩转 OpenStack(64)
  3. AngularJS API之$injector ---- 依赖注入
  4. Util应用程序框架公共操作类(十):可空值类型扩展
  5. spring boot源码分析之SpringApplication
  6. MyEclipse打不开jsp文件 报错“Failed to create the part&#39;s controls&quot;
  7. DotNet指定文件显示的尺寸
  8. Java中的反射和注解
  9. Moon.Orm与其他Orm的技术对比
  10. 记录visual Studio使用过程中的两个问题