安卓自定义属性主要有3个步骤

  1. 在values文件夹新建attrs.xml文件中声明属性,包括属性名和格式,format常用属性有string ,integer,reference等
<?xml version="1.0" encoding="utf-8"?>
<resources><!-- 声明属性集的名称 --><declare-styleable name="MyToggleButtton"><!-- 声明属性的name与类型 --><attr name="my_background" format="reference"/><attr name="my_slide_btn" format="reference"/><attr name="curr_state" format="boolean"/></declare-styleable></resources>
  1. 在布局文件中使用,使用之前必须先声明命名空间,前面是固定不变的内容,后面是包名.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zj="http://schemas.android.com/apk/res/com.zj.switchbutton"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="${relativePackage}.${activityClass}" ><com.zj.switchbutton.MyTrouggleButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"zj:my_background="@drawable/switch_background"zj:my_slide_btn="@drawable/slide_button"zj:curr_state="true"/></RelativeLayout>
  1. 在自定义view的构造方法中,通过解析AttributeSet方法,获得所需要的属性值,解析AttributeSet主要有两种方法

第一种:通过attrs.getAttributeValue获得

int counts=attrs.getAttributeCount();for(int i=0;i<counts;i++){attrs.getAttributeName(i);attrs.getAttributeValue(i);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubiniView(context);String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "mytitle");desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_on");desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_off");tv_title.setText(title);setDesc(desc_off);}

第二种:通过TypedArray获得

public MyTrouggleButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub//获得自定义属性TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyToggleButtton);int N=ta.getIndexCount();for(int i=0;i<N;i++){int itemId=ta.getIndex(i);switch (itemId) {case R.styleable.MyToggleButtton_curr_state:current_state=ta.getBoolean(itemId, false);break;case R.styleable.MyToggleButtton_my_background:backgroundID=ta.getResourceId(itemId, -1);if(backgroundID==-1){throw new RuntimeException("请设置背景图片");}backgroundBitmap=BitmapFactory.decodeResource(getResources(),backgroundID);break;case R.styleable.MyToggleButtton_my_slide_btn:slideButtonID=ta.getResourceId(itemId, -1);if(backgroundID==-1){throw new RuntimeException("请设置图片");}slideBtnBitmap=BitmapFactory.decodeResource(getResources(), slideButtonID);default:break;}}init();}

自定义属性到底有什么用呢?当界面上的自定义元素有一些值需要改变并且大量重复的时候,自定义属性可以有效的提高代码的重用性,下面是一个简单的例子

声明属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="TextView"><attr name="mytitle" format="string" /><attr name="desc_on" format="string" /><attr name="desc_off" format="string" /></declare-styleable>
</resources>

在xml文件中定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zj="http://schemas.android.com/apk/res/com.zj.mobilesafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="55dip"android:background="#8866ff00"android:gravity="center"android:text="设置中心"android:textColor="#000000"android:textSize="22sp" /><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_update"android:layout_width="wrap_content"android:layout_height="65dip"zj:desc_off="设置自动更新已经关闭"zj:desc_on="设置自动更新已经开启"zj:mytitle="设置自动更新" ></com.zj.mobilesafe.ui.SettingItemView><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_show_address"android:layout_width="wrap_content"android:layout_height="65dip"zj:desc_off="设置显示号码归属地已经关闭"zj:desc_on="设置显示号码归属地已经开启"zj:mytitle="设置显示号码归属地" ></com.zj.mobilesafe.ui.SettingItemView><com.zj.mobilesafe.ui.SettingClickViewandroid:id="@+id/scv_changebg"android:layout_width="wrap_content"android:layout_height="65dip"></com.zj.mobilesafe.ui.SettingClickView><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_callsms_safe"android:layout_width="wrap_content"android:layout_height="wrap_content"zj:desc_off="黑名单拦截已经关闭"zj:desc_on="黑名单拦截已经开启"zj:mytitle="黑名单拦截设置" ></com.zj.mobilesafe.ui.SettingItemView><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_watchdog"android:layout_width="wrap_content"android:layout_height="wrap_content"zj:desc_off="看门狗已经关闭"zj:desc_on="看门狗已经开启"zj:mytitle="程序锁设置" ></com.zj.mobilesafe.ui.SettingItemView></LinearLayout>

解析属性并且改变属性

/*** 自定义的组合控件* @author Administrator**/
public class SettingItemView extends RelativeLayout {private CheckBox cb_status;private TextView tv_desc;private TextView tv_title;private  String desc_on;private String desc_off;/*** 初始化布局文件* @param context*/private void iniView(Context context) {// TODO Auto-generated method stubView.inflate(context, R.layout.setting_item_view, SettingItemView.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);}public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubiniView(context);}/*** 带有两个参数的构造方法,布局文件使用的时候调用 * @param context* @param attrs*/public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubiniView(context);String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "mytitle");desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_on");desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_off");tv_title.setText(title);setDesc(desc_off);}public SettingItemView(Context context) {super(context);// TODO Auto-generated constructor stubiniView(context);}/*** * 检验组合和控件是否有焦点*/public boolean isChecked(){return cb_status.isChecked();}/*** 设置组合控件的是否选中*/public void setChecked(boolean checked){if(checked){setDesc(desc_on);}else{setDesc(desc_off);}cb_status.setChecked(checked);}/*** 组合控件 的内容发生改变* */public void setDesc(String text){tv_desc.setText(text);}}

效果如下

Android之自定义属性相关推荐

  1. Android中自定义属性(attrs.xml,TypedArray的使用)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  2. android开发:Android 中自定义属性(attr.xml,TypedArray)的使用

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...

  3. Android筑基——自定义属性详解

    目录 1. 前言 2. 正文 2.1 声明与使用自定义属性的简单例子 创建自定义 View 类 定义自定义属性 在布局文件中使用自定义 View 和自定义属性 解析自定义属性 2.2 合理地声明和解析 ...

  4. android获取自定义属性,android 自定义控件中获取属性的三种方式(转)

    第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值. (1)在xml文件中设置属性值 android:layout_width="f ...

  5. Android之自定义属性,format详解

    1. reference:参考某一资源ID.(1)属性定义:<declare-styleable name = "名称"><attr name = "b ...

  6. [Android自定义控件]自定义属性attrs.xml中format

    前言 在我们自定义控件的时候,需要自己定义布局xml对象属性,就需要styles.xml自定义,然后再自定义java文件中获取信息,记录方便自己使用 使用 xml首先需要自定义命名空间: xmlns: ...

  7. Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:at ...

  8. Android 自定义属性(attrs.xml,TypedArray)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...

  9. Android之内核学习笔记

    0.Android系统启动 <Android系统启动流程 -- bootloader> <The Android boot process from power on> < ...

最新文章

  1. python中的object是什么意思_Python object类中的特殊方法代码讲解
  2. Linux命令行下关机【Ubuntu】
  3. linux内核能否扩展,Linux内核用到的GCC扩展
  4. 硅谷最有名的帮派:如果你不知道PayPal黑帮
  5. python zipfile_python zipfile - 刘江的python教程
  6. 一般动态规划问题合集(Leetcode题解-Python语言)
  7. SkyEye图形化界面使用技巧篇(二)
  8. BTC36 CLUB国际基金拟向美国SEC提交那斯达克股票上市申请
  9. JavaScript学习(四十八)—原型对象的增删改查
  10. 在线打字练习网站介绍
  11. 登录oneNote失败解决
  12. 网吧用电影服务器系统,网吧流媒体电影服务器搭建的解决方案
  13. 9106w android7,三星note4 SM-N9106W原厂刷机包4.4.4/5.0.1rom线刷包Root驱动
  14. Oracle查询优化
  15. 年度最火的AOA蓝牙室内定位原理
  16. 飞书和钉钉之间,差了几个企业微信?
  17. 【火炉炼AI】机器学习008-简单线性分类器解决二分类问题
  18. 电脑D盘格式化了怎么恢复
  19. autojs ui界面漂亮模板2
  20. 单点登录、统一认证解决方案(一)

热门文章

  1. 阿里Java架构师精通资料:性能优化+亿级并发架构汇总+架构选型
  2. 最全蚂蚁金服高级Java面试题目(3面)
  3. 安装kenlm出现问题的解决方案gcc g++
  4. 11 操作系统第三章 内存管理 内存的基本知识 内存管理 内存空间扩充 连续分配管理方式
  5. Hadoop之Shell脚本自动启动
  6. 管理开机启动:chkconfig
  7. BZOJ3245: 最快路线 拆点dijkstra
  8. git编译安装与常见问题解决
  9. 设置MYSQL数据库编码为UTF-8
  10. C#实验——Problem Statement