Android之自定义属性

安卓自定义属性主要有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);}}

效果如下

这里写图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/494387.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

log4j的配置方法

一、 介绍 Log4j是Apache的一个开放源代码项目&#xff0c;通过使用Log4j&#xff0c;我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务 器、NT的事件记录器、UNIX Syslog守护进程等&#xff1b;我们也可以控制每一条日志的输出格式&#xff1b;通…

中国智能制造发展趋势!

来源&#xff1a;中国电子信息产业发展研究院赛迪顾问认为&#xff0c;我国智能制造发展有三个趋势&#xff0c;即企业向系统方案解决供应商转型&#xff0c;生产更加柔性化、定制化&#xff0c;“互联网”促进价值链向价值网转变。一、智能制造装备企业逐步向系统方案解决供应…

【每日SQL打卡】​​​​​​​​​​​​​​​DAY 18丨即时食物配送 I【难度简单】​

【未来的你&#xff0c;会感谢今天努力的你】每日两题&#xff0c;一难一易&#xff0c;每天进步一点点&#xff0c;可能会直接导致一场面试的成功&#xff0c;或工作的轻松搞定&#xff0c;从而升职加薪迎娶白富美&#xff0c;加油小伙伴&#xff01; &#x1f345;举办场地&a…

感知哈希算法原理与实现

今天忽然想做一个图像识别的APP&#xff0c;但是在两张图片相似度的问题上产生了问题&#xff0c;感知哈希算法并不能解决这个问题&#xff0c;只是我在试着解决问题的过程中学到的一点知识。 这里的关键技术叫做”感知哈希算法”&#xff08;Perceptual hash algorithm&#…

Android开发中目前流行控件和知识点总结

1、SlidingMenu 滑动菜单 应用案例&#xff1a;Facebook 、 Path 2.0 、人人、网易新闻 下载地址&#xff1a; https://github.com/jfeinstein10/SlidingMenu 2、PullToRefresh 下拉刷新 应用案例&#xff1a;新浪微博 等等 &#xff0c;这个很长用哦&#xff01; 下载地址&…

李德毅:“反用驾驶脑”测认知能力,谁说酒驾一定违规?

来源&#xff1a;德先生摘要&#xff1a;从2016年阿尔法狗成功挑战人类智慧的这场世纪大战开始&#xff0c;“人工智能”便引发了全世界的关注。从2016年阿尔法狗成功挑战人类智慧的这场世纪大战开始&#xff0c;“人工智能”便引发了全世界的关注。为了让中国在人工智能这场“…

【每日SQL打卡】​​​​​​​​​​​​​​​DAY 18丨即时食物配送 II【难度中等】​

【未来的你&#xff0c;会感谢今天努力的你】每日两题&#xff0c;一难一易&#xff0c;每天进步一点点&#xff0c;可能会直接导致一场面试的成功&#xff0c;或工作的轻松搞定&#xff0c;从而升职加薪迎娶白富美&#xff0c;加油小伙伴&#xff01; &#x1f345;举办场地&a…

[zz]4.1.5 进程的处理器亲和性和vCPU的绑定

转载自&#xff1a;http://smilejay.com/2012/08/kvm-vcpu-binding/ 通常情况下&#xff0c;在SMP系统中&#xff0c;Linux内核的进程调度器根据自有的调度策略将系统中的一个进程调度到某个CPU上执行。一个进程在前一个执行时间 是在cpuM&#xff08;M为系统中的某CPU的ID&…

谷歌无人车十年记:理想背后的骨感现实 | 厚势汽车

来源&#xff1a;The Information编译&#xff1a;厚势摘要&#xff1a;即使是近 10 年后&#xff0c;Waymo 仍远没有实现真正的 L4&#xff0c;即使是在相对简单的郊区环境下。「全自动驾驶的汽车来了&#xff01;」去年 11 月&#xff0c;Waymo 搞了一个大新闻。公司宣布自己…

【每日SQL打卡】​​​​​​​​​​​​​​​DAY 19丨行转列【难度中等】​

【未来的你&#xff0c;会感谢今天努力的你】每日两题&#xff0c;一难一易&#xff0c;每天进步一点点&#xff0c;可能会直接导致一场面试的成功&#xff0c;或工作的轻松搞定&#xff0c;从而升职加薪迎娶白富美&#xff0c;加油小伙伴&#xff01; &#x1f345;举办场地&a…

自定义控件之瀑布流与水波纹实现

本文主要讲述了利用android自定义控件实现瀑布流与水波纹效果 首先为实现效果&#xff0c;应了解touch事件在android中的传递机制 在执行touch事件时 首先执行dispatchTouchEvent方法&#xff0c;执行事件分发。 再执行onInterceptTouchEvent方法&#xff0c;判断是否中断事件…

医学信息学相关术语、缩语及专业名词

医学信息学相关术语、缩语及专业名词 很棒哦&#xff0c;分享了&#xff0c;需要的可以去瞅瞅http://www.med-informatics.cn/MedInfo_gloss/medinfo_gloss_p1.htm如果你到它的完整上再逛逛&#xff0c;发觉还有很多好资源&#xff0c;譬如&#xff1a;http://www.med-informat…

【每日SQL打卡】​​​​​​​​​​​​​​​DAY 19丨最后一个能进入电梯的人【难度中等】​

【未来的你&#xff0c;会感谢今天努力的你】每日两题&#xff0c;一难一易&#xff0c;每天进步一点点&#xff0c;可能会直接导致一场面试的成功&#xff0c;或工作的轻松搞定&#xff0c;从而升职加薪迎娶白富美&#xff0c;加油小伙伴&#xff01; &#x1f345;举办场地&a…

神经网络并不是尚方宝剑,我们需要正视深度 NLP 模型的泛化问题

来源&#xff1a;AI 科技评论前段时间的文章《顶会见闻系列&#xff1a;ACL 2018&#xff0c;在更具挑战的环境下理解数据表征及方法评价》中&#xff0c;我们介绍了 ACL 大会上展现出的 NLP 领域的最新研究风向和值得关注的新进展。从这些新动向上我们似乎应该对深度学习 NLP …

Android之XML序列化和解析

XML文件是一种常用的文件格式&#xff0c;可以用来存储与传递数据 &#xff0c;本文是XML文件序列化与解析的一个简单示例 写文件到本地&#xff0c;并用XML格式存储 /*** 写xml文件到本地*/private void writeXmlToLocal() {List<Person> personList getPersonList()…

北京智控美信(长春)数据中心应聘总结

时间&#xff1a; 2012年9月12日 公司介绍&#xff1a; IT外包公司&#xff0c;提供咨询——开发——维护的整套服务。涉及金融、石油、电力等行业。 代表性项目&#xff1a; 吉林银行的先进性系统。 简历投递&#xff1a; 发送到邮箱&#xff1a;kd.hrinfohold.com.cn (注明应…

信息技术智库丨月度大考试

【未来的你&#xff0c;会感谢今天努力的你】每日两题&#xff0c;一难一易&#xff0c;每天进步一点点&#xff0c;可能会直接导致一场面试的成功&#xff0c;或工作的轻松搞定&#xff0c;从而升职加薪迎娶白富美&#xff0c;加油小伙伴&#xff01; &#x1f345;举办场地&a…

Android之查看网络图片和网页HTML

网络编程是Android应用中很重要的一部分&#xff0c;本文主要讲述了利用HttpURLConnection获取网络图片和HTML的方法。 获取网络图片 public class MainActivity extends Activity implements OnClickListener {private static final String TAG "MainActivity";p…

【每日SQL打卡】​​​​​​​​​​​​​​​DAY 20丨查询球队积分【难度中等】​

【未来的你&#xff0c;会感谢今天努力的你】每日两题&#xff0c;一难一易&#xff0c;每天进步一点点&#xff0c;可能会直接导致一场面试的成功&#xff0c;或工作的轻松搞定&#xff0c;从而升职加薪迎娶白富美&#xff0c;加油小伙伴&#xff01; &#x1f345;举办场地&a…

来博客园开了个博客

平时搜索资料也经常搜到博客园的文章&#xff0c;我一看&#xff0c;支持代码高亮&#xff0c;就注册了&#xff0c;也开了个。 以前辗转好多个博客&#xff0c;都没坚持&#xff0c;后来都废弃了。转载于:https://www.cnblogs.com/dyllen/archive/2013/01/31/2887383.html