[Android]自定义组件示例:使用attrs.xml文件定制RadioButton
2014-01-06 10:53
501 查看
1.在res/values下创建attrs.xml [html] view plaincopy <declare-styleable name="MyRadioButton"> <attr name="str" format="string"/> </declare-styleable> MyRadioButton为组件名字,随意起,attr标签定义组件的属性,name对应的是属性名,format是属性的类型,具体可参见《[Android]attrs.xml文件中属性类型format值的格式》。 2.在自定义的组件中使用attrs.xml文件的定义 [java] view plaincopy 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.在布局文件中使用 [html] view plaincopy <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文件定制RadioButton
- android应用面试宝典(下);用自定义组件控制循环组件使用bug还添加标志显示答案标志位;通过包名读取配置文件AndroidManifest.xml
- Android中使用attrs.xml文件定制
- Android 自定义View及其在布局文件中的使用示例(二)
- Android 自定义View及其在布局文件中的使用示例
- android自定义Button,通过在XML布局文件里直接使用自定义参数
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
- Android 自定义View及其在布局文件中的使用示例
- Android 自定义View及其在布局文件中的使用示例
- Android 自定义View及其在布局文件中的使用示例
- 在android style.xml文件中使用自定义属性
- 【Android】 XML布局文件中,使用自定义属性不提示和不生效
- Android中使用XML自定义组件各种状态下的背景图片
- 【整理】Android中使用XML自定义组件各种状态下的背景图片
- Android自定义模拟器skin 自定义属性(attrs.xml,TypedArray)的使用
- Android 自定义View及其在布局文件中的使用示例
- Android之Merge及自定义属性attrs.xml使用 推荐
- android 自定义属性的使用一、在res/values文件下定义一个attrs.xml文件,代码如下
- Android中自定义属性的使用------res/values文件下定义一个attrs.xml
- Android,xml组件属性style的使用,自定义适配器(含持有者模式)