[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 ...
随机推荐
- java中如何将JScrollPane的垂直滚动条自动移动到最下端
JPanel QQP = new JPanel(); JScrollPane jsp = new JScrollPane(QQP); JScrollBar jsb = jsp.getVerticalS ...
- OP和DBA相关的一些有用资源
最近国外blog上看到的一片资源分享博文,精而全,于是转帖分享 Must-Read Books List First of all, I would like to share a list of b ...
- BZOJ1003 物流运输 最短路+DP
1003: [ZJOI2006]物流运输 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条 ...
- f4: Facebook’s Warm BLOB Storage System——Erasure Code
Facebook在OSDI 2014上发表论文f4: Facebook's Warm BLOB Storage System,这个系统主要目的就是降低存储成本,在容忍磁盘,主机,机架,数据中心的同时提 ...
- Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用
最近一直在做一些技术性的研究和框架改进工作,博客也落下好几天没有更新了,也该是时候静下心来,总结这段时间的一些技术改进的经验了.和上一阶段的CRM系统开发和技术研究一样,我都喜欢在一个项目或者模块完成 ...
- SVN 忽略文件但不删除文件
SVN忽略一些不必要的文件但不删除 如果svn仓库中有一些不希望被别人提交的文件 该如何忽略掉对这个文件的更改但又不删除这个文件呢? 在找了一堆解决方案后得出了如下结论 去除要被忽略文件的版本控制 基 ...
- 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件
[源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...
- string.join加引号
columnsGen = string.Join(",", modelDictionary.Keys); valueGen = modelDictionary.Values.Agg ...
- strtr对用户输入的敏感词汇进行过滤
/** * 过滤用户输入的基本数据,防止script攻击 * * @access public * @return string */ function compile_str($str) { $ar ...
- CSS应用内容补充及小实例
一.clear 清除浮动 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...