当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] Android中使用attrs.xml文件定制RadioButton1.在res/values下创建attrs.xml 1 <declare-styleable name="MyRadioButton">

 Android中使用attrs.xml文件定制RadioButton

1.在res/values下创建attrs.xml

1 <declare-styleable name="MyRadioButton">
2         <attr name="str" format="string"/>
3 </declare-styleable>

MyRadioButton为组件名字,随意起,attr标签定义组件的属性,name对应的是属性名,format是属性的类型,具体可参见《 [Android]attrs.xml文件中属性类型format值的格式》。

2.在自定义的组件中使用attrs.xml文件的定义

01 public class MyRadioButton extends RadioButton {
02     private String url;
03       
04     public MyRadioButton(Context context, AttributeSet attrs) {
05         super(context, attrs);
06         TypedArray taArray = context.obtainStyledAttributes(attrs,R.styleable.MyRadioButton);
07         this.url = taArray.getString(R.styleable.MyRadioButton_str);
08         taArray.recycle();
09     }
10   
11     public String getUrl() {
12         return url;
13     }
14   
15     public void setUrl(String url) {
16         this.url = url;
17     }    
18   
19 }

a. TypedArray是存放资源R.styleable.MyRadioButton指定的属性集合。

b. 通过getXXX()获取属性值。

c. recycle()结束绑定 3.在布局文件中使用

01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     xmlns:demo="http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"
03     android:layout_width="fill_parent"
04     android:layout_height="fill_parent"
05     android:orientation="vertical" >
06     <RadioGroup
07        android:layout_width="fill_parent"
08        android:layout_height="wrap_content"
09        >
10         <net.csdn.blog.wxg630815.MyRadioButton
11             android:layout_width="fill_parent"
12             android:layout_height="wrap_content"
13             android:id="@+id/myradio1"
14             demo:str="1.csdn.net"
15             />
16         <net.csdn.blog.wxg630815.MyRadioButton
17             android:layout_width="fill_parent"
18             android:layout_height="wrap_parent"
19             android:id="@+id/myradio2"
20             demo:str="2.csdn.net"
21             />
22          
23    </RadioGroup>
24   
25 </LinearLayout>

注意: xmlns:demo="http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"

只有声明这句以后,url属性才会被布局文件识别。net.csdn.blog.wxg630815指的是AndroidManifest.xml文件中manifest元素的package属性值。

使用demo:str给url赋值。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

罗德与施瓦茨与SmartViser携手开发了一种用于测试符合欧盟销售的智能手机和平板电脑的新Energy Efficiency Index(EEI)标签法规的解决方案。该解决方案的核心是R&S CMX500,这是...

关键字: 智能手机 Android iOS

北京2023年9月22日 /美通社/ -- 华为全联接大会2023期间,在以"研发数字化和智慧生产,提速企业自主创新"专项论坛上,软通动力数字化创新服务线高级副总裁、数字基础设施与集成总经理谢睿,软通...

关键字: 智能化 华为云 数字化 RS

(全球TMT2023年9月15日讯)企业移动性管理领域企业42Gears宣布推出AstroStatus,这是一种直观的事件沟通解决方案,旨在增强企业能力,让企业在云托管解决方案出现服务中断和意外故障时能随时向客户通报情...

关键字: STATUS RS OS GE

印度班加罗尔2023 9月15日 /美通社/ -- 企业移动性管理领域的领先企业 42Gears 十分高兴地宣布推出AstroStatus,这是一种直观的事件沟通解...

关键字: STATUS RS OS GE

上海2023年9月14日 /美通社/ -- 自CAR-T疗法在国内获批上市以来,上海嘉会国际肿瘤中心携手国内外合作伙伴,在临床经验分享、学术进展交流、血液肿瘤宣教等方面积极共建CAR-T国际诊疗平台;同时,嘉会...

关键字: IC 模拟 VI RS

(全球TMT2023年9月7日讯)思享无限控股有限公司宣布,将以300万美元投资DVCC TECHNOLOGY L.L.C(DVCC),以获取其30%股权。这一重要举措标志着思享无限从移动娱乐向元宇宙生活方式的转变,也...

关键字: DVCC SE RS AI

北京2023年9月6日 /美通社/ -- 思享无限控股有限公司(以下简称:思享无限,纳斯达克股票代码:SJ)对外宣布,将以300万美元投资DVCC TECHNOLOGY L.L.C(以下简称DVCC),以获取其30%股权...

关键字: DVCC TECHNOLOGY SE RS

(全球TMT2023年9月4日讯)当地时间9月1日,荣耀终端有限公司CEO赵明在2023德国柏林消费电子展(Internationale Funkausstellung Berlin,IFA)开幕日发表题为《展开未来(...

关键字: 荣耀 折叠屏手机 SE RS

柏林2023年9月4日 /美通社/ -- 当地时间9月1日,荣耀终端有限公司CEO赵明在2023德国柏林消费电子展(Internationale Funkausstellung Berlin,以下简称IFA)开幕日发表题...

关键字: 荣耀 折叠屏 SE RS

(全球TMT2023年8月24日讯)2023年8月23日,时值实时3D引擎Unity在华设立合资公司Unity中国一周年之际,Unity中国正式推出Unity中国版引擎——团结引擎。Unity全球CEO John Ri...

关键字: UNITY CE Android 开发者
关闭
关闭