当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 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赋值。

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

柏林2025年9月6日 /美通社/ -- 柏林当地时间9月4日,在德国柏林国际电子消费品展览会(International Funkausstellung Berl...

关键字: 智能家电 RS 测试 扫地机器人

其他电脑(比如安卓手机/平板电脑)的屏幕坏了,你可能想在安排维修之前紧急访问一些东西。你可以使用android的USB OTG功能(是的,几乎每个android都支持这个功能,你可以将鼠标和键盘连接到它)。

关键字: USB 鼠标 Android 树莓派

上海 2025年7月14日 /美通社/ -- 全球财会专业组织ACCA(特许公认会计师公会)今日荣幸宣布,第二十二届全国就业力X未来商业创想大赛圆满落下帷幕,上海财经大学代表队摘得全国总冠军,西南科技大学斩获全国亚军,...

关键字: 大赛 AC 可持续发展 RS

双方签署预防医学联合研究合作协议  提高对可预防疾病的认知  降低医疗成本  德国柏林 2025年7月9日 /美通社/ -- 西门子医疗和柏林夏里特医学院 (Charité – Univers...

关键字: 西门子 BSP 人工智能 RS

BrowserStack推出AI代理套件,以大规模重新定义软件质量 都柏林 2025年7月1日 /美通社/ -- 全球领先的软件测试平台BrowserStack今日宣布推...

关键字: BROWSE STACK RS AI

巴黎和首尔 2025年6月12日 /美通社/ -- 全球AI内容创作先锋SKAI Intelligence今日宣布,将于2025年巴黎VivaTech科技盛会正式发布全球首个完全基于NVIDIA Omniverse构建...

关键字: AI NVIDIA SE RS

Google 宣布与中国 AR 科技公司 XREAL 达成深度战略合作,联合推出全球首款专为 Android XR 平台打造的旗舰级 AR 眼镜 Project Aura。

关键字: Google XREAL Android XR眼镜 AR

休斯顿 2025年5月16日 /美通社/ -- 具身智能领域的领导者Persona AI今日宣布,已完成超额认购的预种子轮融资,融资金额达2700万美元。 这笔巨额融资将加...

关键字: 机器人 RS AI API

上海 2025年5月15日 /美通社/ -- 近日,全球顶尖商业地产服务及投资管理公司高力国际(纳斯达克/多伦多证交所代码:CIGI)宣布,凭借行业优势资源及专业服务,成功协助国内芯片领域龙头企业乐鑫科技(上交所:68...

关键字: 芯片设计 RS 人工智能 网络
关闭