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赋值。

更多相关文章

  1. 让你明白,Android常用组件四大天王
  2. android 根文件系统 init
  3. .net程序员转战android第一篇---环境部署
  4. Android中jar包封装及调用中出现的问题及解决方案
  5. ANT学习、keystore制作,APK签名
  6. Android(安卓)studio调用第三方aidl
  7. Android开发-Android(安卓)Jetpack 架构组件的使用(一)
  8. Android(安卓)MultiDex
  9. Android(安卓)列表中设置Button后setOnItemClickListener失效问

随机推荐

  1. Android中程序与Service交互的方式——交
  2. Android基础(一) ImageButton
  3. Android(安卓)显示Intent和隐示Intent
  4. Android 将drawable下的图片转换成bitmap
  5. 小程序版 玩Android(安卓)客户端
  6. Android(安卓)基础教程之-------Android(
  7. [原]Android应用程序在新的进程中启动新
  8. [置顶] android下调试声卡驱动之概述
  9. Android将获取到文件的uri转换为字符串的
  10. Android(安卓)AIDL远程调用