Android中使用attrs.xml文件定制RadioButton
1.在res/values下创建attrs.xml
<declare-styleable name="MyRadioButton"> <attr name="str" format="string"/> </declare-styleable>
MyRadioButton为组件名字,随意起,attr标签定义组件的属性,name对应的是属性名,format是属性的类型,具体可参见《 [Android]attrs.xml文件中属性类型format值的格式》。
2.在自定义的组件中使用attrs.xml文件的定义
public class MyRadioButton extends RadioButton { private String url; public MyRadioButton(Context context, AttributeSet attrs) { super(context, attrs); TypedArray taArray = context.obtainStyledAttributes(attrs,R.styleable.MyRadioButton); this.url = taArray.getString(R.styleable.MyRadioButton_str); taArray.recycle(); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
a. TypedArray是存放资源R.styleable.MyRadioButton指定的属性集合。
b. 通过getXXX()获取属性值。
c. recycle()结束绑定 3.在布局文件中使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:demo="http://schemas.android.com/apk/res/net.csdn.blog.wxg630815" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" > <net.csdn.blog.wxg630815.MyRadioButton android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/myradio1" demo:str="1.csdn.net" /> <net.csdn.blog.wxg630815.MyRadioButton android:layout_width="fill_parent" android:layout_height="wrap_parent" android:id="@+id/myradio2" demo:str="2.csdn.net" /> </RadioGroup> </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赋值。
相关推荐
Android中自定义属性attrs.xml、TypedArray的使用,只是做的一个示例,不喜勿喷
解决error: No resource identifier found for attribute 'cardCornerRadius' in package 'com.example.test' android\sdk\extras\android\support\v7\cardview\res\values\attrs.xml 把这个拷贝到工程目录下就有了
Android自定义View中attrs.xml的实例详解 我们在自定义View的时候通常需要先完成attrs.xml文件 在values中定义一个attrs.xml 然后添加相关属性 这一篇先详细介绍一下attrs.xml的属性。 <?xml version=1.0 ...
asm-attrs.jar asm-attrs.jar asm-attrs.jar
标签:asm-attrs.jar.zip,asm,attrs,jar.zip包下载,依赖包
Android自定义控件的使用与自定义控件中使用自定义attrs.xml属性标签
从 Android attrs XML 生成 Java 枚举类介绍(和警告) 这是一个黑客。 而已。 我敢肯定,拥有比我更多 Gradle 技能的人可能可以将它变成一个合适的插件,但是如果你需要一些快速而肮脏的东西,试试这个……但如果它...
1、attrs.xml 文件 和 R 文件对应关系 2、attrs.xml 文件重点注意 (1)declare-styleable子元素 (2)attrs.xml 文件只做属性的描述 步骤2:写自定义类 步骤3:使用自定义组件、自定义属性 三、效果展示-简单小结 1...
前端大厂最新面试题-data_object_add_attrs.docx
│ │ attrs.png │ │ demo1.gif │ │ demo2.gif │ │ icon.png │ │ Thumbs.db │ │ │ ├─gradle │ │ └─wrapper │ │ gradle-wrapper.jar │ │ gradle-wrapper.properties │ │ │ └...
标签:asm-attrs-1.4.3.jar.zip,asm,attrs,1.4.3,jar.zip包下载,依赖包
测试:Android 中自定义属性(attr.xml,TypedArray)的使用 注意:MyView(Context context,AttributeSet attrs)构造函数的实现, 和注意main.xml的LinearLayout 里加的声明 要了解:test:textSize="20px" test:...
标签:asm-attrs-1.5.3.jar.zip,asm,attrs,1.5.3,jar.zip包下载,依赖包
标签:asm-attrs-2.0.jar.zip,asm,attrs,2.0,jar.zip包下载,依赖包
自定义view起步 1,新建attrs.xml文件,增加declare-styleable 2,实现android.view.View 3,在layout.xml 使用自定义view
http://www.jar114.com/s/?keyword=asm-attrs
Eclipse:下载项目,将library里面的VerificationCodeView.java跟res/values/attrs.xml文件拷贝进项目。 二、用法 在布局中集成,注意需要在布局的根节点添加命名xmlns:zhangym=...