展会信息港展会大全

Android中使用attrs.xml文件定制RadioButton
来源:互联网   发布日期:2015-12-04 16:28:13   浏览:2218次  

导读:Android中使用attrs.xml文件定制RadioButton1.在res/values下创建attrs.xmldeclare-styleable name=MyRadioButton attr name=str format=string//declare-styleableMyRadioButton......

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

赞助本站

人工智能实验室

相关热词: attrs.xml 定制 RadioButton

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港