• 在res/values下创建attrs.xml


    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文件的定义

    1. public class MyRadioButton extends RadioButton {  
    2.     private String url;  
    3.       
    4.     public MyRadioButton(Context context, AttributeSet attrs) {  
    5.         super(context, attrs);  
    6.         TypedArray taArray = context.obtainStyledAttributes(attrs,R.styleable.MyRadioButton);  
    7.         this.url = taArray.getString(R.styleable.MyRadioButton_str);  
    8.         taArray.recycle();  
    9.     }  
    10.   
    11.     public String getUrl() {  
    12.         return url;  
    13.     }  
    14.   
    15.     public void setUrl(String url) {  
    16.         this.url = url;  
    17.     }  
    18.       
    19.   
    20. }  

    a. TypedArray是存放资源R.styleable.MyRadioButton指定的属性集合。
    b. 通过getXXX()获取属性值。
    c. recycle()结束绑定

     

    3.在布局文件中使用

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:demo="http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.     <RadioGroup   
    7.        android:layout_width="fill_parent"  
    8.        android:layout_height="wrap_content"  
    9.        >  
    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>  
  • 相关阅读:
    *Path Sum II
    *Path Sum
    Same Tree
    Symmetric Tree
    hprof网络连接
    gc
    java thread park
    sudo !!
    ecb gud
    jpda
  • 原文地址:https://www.cnblogs.com/carbs/p/2580069.html
Copyright © 2020-2023  润新知