上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性

上一节组合控件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] 手机卫士自定义控件的属性的更多相关文章

  1. Android 手机卫士--自定义控件(获取焦点的TextView)

    本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,转载请注明源地址. 本文将实现标题栏下面的textview中的文字跑马灯的效果,就是将一行文字水 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. [android] 手机卫士自定义滚动控件

    TextView控件设置单行显示 android:singleLine=”true” 设置TextView开始的位置显示省略号,android:ellipsize=”start” 设置滚动属性,and ...

随机推荐

  1. 链表中倒数第k个结点

    题目: 输入一个链表,输出该链表中倒数第k个结点. 思路: 因为是单向链表,如果使用最普通的遍历来解决的话会多出很多不必要的遍历.有一个比较好的解法,设置两个指针两个指针之间差k-1个位置,也就是当后 ...

  2. NuGet 让你都美好的PM

    题外话 从前有座山,山上有座庙,庙里有个老和尚.阿阿阿,好多鱼好多余. 什么是Nuget NuGet(发音:New-Get)是一个Visual Studio的扩展.在使用Visual Studio开发 ...

  3. CSS魔法堂:Position定位详解

    一.Position各属性值详解   1.  static :默认值,元素将按照正常文档流规则排列.   2.  relative :相对定位,元素仍然处于正常文档流当中,但可以通过left.top. ...

  4. CentOS6.5菜鸟之旅:安装Realtek无线网卡驱动

    一.前言 CentOS6.5不像CentOS7和Unbuntu那样自动安装好了无线网卡驱动,因此需要我们折腾一下. 二.安装前的准备工作   [a] 检查无线网卡驱动的安装情况(通过查看网络接口的安装 ...

  5. EF错误记录

    纯属个人记录错误使用: 1.EntityType“area”未定义键.请为该 EntityType 定义键. 产生原因: 1.命名空间引用错误,可能命名重复导致引用错误 2.实体类无法识别主键或者未设 ...

  6. 登陆mysql时提示异常的解决方法

    [root@host2 ~]# mysql -uroot -p Enter password: ERROR (HY000): Can't connect to local MySQL server t ...

  7. Spring基础——在Spring Config 文件中配置 Bean

    一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...

  8. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

  9. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  10. 文本对象模型(Document Object Model)

    本文内容: 1. 概述 2. DOM中的节点类型 3. DOM节点的选取 4. 存取元素属性 5.DOM元素的增删 6.小结 ★ 概述 文本对象模型(DOM)是一个能够让程序和脚本动态访问和更新文档内 ...