[android] 手机卫士自定义控件的属性
上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性
上一节组合控件SettingItemView中有三个控件,分别是TextView大标题,TextView描述,CheckBox复选框
自定义属性 tsh:title=”大标题” 和tsh:desc_on=”小标题开启”,tsh:desc_off=”小标题关闭”
添加命名空间,xmlns:tsh=”http://schemas.android.com/apk/res/包名"
在res/values/目录下创建 attrs.xml文件
添加节点 <declare-styleable name=”TextView”>
节点下添加节点<attr name=”title” format=”string”/>,添加其他两个属性的节点
在布局文件使用的时候,会调用带有两个参数的构造方法
在这个构造方法里面,会传递一个AttributeSet对象
调用AttributeSet对象的getAttributeValue()方法,得到属性值,参数:索引位置,不推荐
调用AttributeSet对象的getAttributeValue(namespace,name)方法,参数:命名空间,属性名
调用TextView对象的setText()方法,直接给设置进去
描述部分,在setChecked()方法里面,判断,再设置
SettingItemView.java
package com.qingguow.mobilesafe.ui; import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView; import com.qingguow.mobilesafe.R; 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;
/**
* 初始化View对象
* @param context
*/
private void initView(Context context) {
View.inflate(context, R.layout.setting_item_view, this);
cb_status=(CheckBox) this.findViewById(R.id.cb_status);
tv_desc=(TextView) this.findViewById(R.id.tv_desc);
tv_title=(TextView) this.findViewById(R.id.tv_title); }
/**
* 判断是否选中
* @return
*/
public boolean isChecked(){
return cb_status.isChecked();
}
/**
* 设置是否选中
* @param status
*/
public void setChecked(boolean status){
if(status){
tv_desc.setText(desc_on);
}else{
tv_desc.setText(desc_off);
}
cb_status.setChecked(status);
}
/**
* 设置显示文本
* @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);
//获取传递的属性
String title=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "title");
tv_title.setText(title);
desc_on=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_on");
desc_off=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_off");
} public SettingItemView(Context context) {
super(context);
initView(context);
} }
activity_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tsh="http://schemas.android.com/apk/res/com.qingguow.mobilesafe"
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="设置中心"
android:textSize="20sp" /> <com.qingguow.mobilesafe.ui.SettingItemView
tsh:title="设置自动更新"
tsh:desc_on="设置自动更新开启"
tsh:desc_off="设置自动更新关闭"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/siv_item">
</com.qingguow.mobilesafe.ui.SettingItemView> </LinearLayout>
attrs.xml
<?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>
[android] 手机卫士自定义控件的属性的更多相关文章
- Android 手机卫士--自定义控件(获取焦点的TextView)
本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,转载请注明源地址. 本文将实现标题栏下面的textview中的文字跑马灯的效果,就是将一行文字水 ...
- Android 手机卫士--参照文档编写选择器
本文来实现<Android 手机卫士--导航界面1的布局编写>中的图片选择器部分的代码. 本文地址:http://www.cnblogs.com/wuyudong/p/5944356.ht ...
- Android 手机卫士--设置界面&功能列表界面跳转逻辑处理
在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...
- Android 手机卫士--确认密码对话框编写
本文接着实现“确认密码”功能,也即是用户以前设置过密码,现在只需要输入确认密码 本文地址:http://www.cnblogs.com/wuyudong/p/5940718.html,转载请注明出处. ...
- Android 手机卫士--签名文件说明&包名说明
在<Android 手机卫士--打包生成apk维护到服务器>一文中,实现了新版本的apk到服务器,当打开客户端apk的时候,发现有新版本,提示更新.还实现了利用xutils工具实现了从服务 ...
- Android 手机卫士--弹出对话框
在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...
- android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码
Android精选源码 android身份证.银行卡号扫描源码 android仿bilibili客户端 android一款3D 指南针 源码 android手机卫士app源码 android提醒应用, ...
- Android 手机卫士--阶段小结1
本文地址:http://www.cnblogs.com/wuyudong/p/5904528.html,转载请注明源地址. 本文对之前手机卫士开发进行一个小结. 1.SplashActivity 版本 ...
- [android] 手机卫士自定义滚动控件
TextView控件设置单行显示 android:singleLine=”true” 设置TextView开始的位置显示省略号,android:ellipsize=”start” 设置滚动属性,and ...
随机推荐
- c# File 操作
//1.---------文件夹创建.移动.删除--------- //创建文件夹 Directory.CreateDirectory(Server.MapPath("a")); ...
- Asp.net Mvc4 基于Authorize实现的模块权限验证方式
在MVC中,我们可以通过在action或者controller上设置Authorize[Role="xxx"] 的方式来设置用户对action的访问权限.显然,这样并不能满足我们的 ...
- css优先级和层叠
css优先级和层叠 1.优先级 计算方法: a.行内样式 b.id选择器的数量 c.类,伪类和属性选择器的数量 d.标签选择器和伪元素选择 ...
- SystemTap知识(一)
SystemTap是一个系统的跟踪探测工具.它能让用户来跟踪和研究计算机系统在底层的实现. 安装SystemTap需要为你的系统内核安装-devel,-debuginfo,-debuginfo-com ...
- SpringMVC从Controller跳转到另一个Controller(转)
http://blog.csdn.net/jackpk/article/details/44117603 [PK亲测] 能正常跳转的写法如下: return "forward:aaaa/bb ...
- ASP.NET 运行时详解 揭开请求过程神秘面纱
对于ASP.NET开发,排在前五的话题离不开请求生命周期.像什么Cache.身份认证.Role管理.Routing映射,微软到底在请求过程中干了哪些隐秘的事,现在是时候揭晓了.抛开乌云见晴天,接下来就 ...
- 2015年百度之星初赛(1) --- B 找连续数
找连续数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别
[源码下载] 重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
- git 给远程库 添加多个url地址
目录[-] 前提 使用流程 原理解析 注意 Other 参考文章 作者:shede333主页:http://my.oschina.net/shede333 && http://blo ...