android 自定属性的使用
来源:互联网 发布:淘宝美工工具 编辑:程序博客网 时间:2024/05/16 17:31
declare-styleable是给自定义控件添加自定义属性用的
1.首先,先写attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="TestAttr"> <attr name="name" format="reference" /> <attr name="age"> <flag name="child" value="10" /> <flag name="young" value="18" /> <flag name="oldman" value="60" /> </attr> <attr name="textSize" format="dimension" /> </declare-styleable></resources>
reference指的是是从string.xml引用过来flag是自己定义的,类似于 android:gravity="top"
dimension 指的是是从dimension.xml里引用过来的内容.注意,这里如果是dp那就会做像素转换
2.在布局文件里的写法
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >s <com.arlos.attrstest.MyTestView android:id="@+id/tvTest" android:layout_width="fill_parent" android:layout_height="wrap_content" attrstest:name="@string/myname" android:gravity="top" attrstest:age="young" attrstest:textSize="@dimen/aa" android:text="@string/hello" /></LinearLayout>
2.1 先引用这个dtd
xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest"
attrstest是随便写的.后面的包名是你所在的项目的根包.也就是在manifest里的com.arlos.attrstest 2.2 在自定义的控件里写属性3. 最后在控件的构造方法里取得这些值
public class MyTestView extends TextView { public MyTestView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.TestAttr); String name = tArray.getString(R.styleable.TestAttr_name); System.out.println("name = " + name); int age = tArray.getInt(R.styleable.TestAttr_age, 200); System.out.println("age = " + age); float demin = tArray.getDimension(R.styleable.TestAttr_textSize,0); System.out.println("demin = " + demin); tArray.recycle(); }}
0 0
- android 自定属性的使用
- android中一个自定属性的xml错误
- 自定属性
- WPF 在ResourceDictionary文件中使用自定的属性作为trigger 的target
- android 双击事件的自定
- android 自定标题栏的应用
- 使用自定的Adapter显示数据
- Android 自定对话框
- 获取select标签下option的自定属性值
- 自定控件设置控件属性用到的一些类
- Android framework 使用自定的activity取代默认的Launcher界面
- Android-自定義控件使用(方便修改控件的風格,只修改布局文件)
- 自定的日历
- Android build.prop 添加自定义项 及使用
- listView 的使用 (自定应使用Adapter)
- Android自定view的一些方法的说明
- Android自定義Dialog
- Android 自定Application类
- 查数据库中所有表名中含某个字符串的表的名称及字段信息sql脚本
- 我的编程学习之路
- Logging from multiple processes using log4net
- 合并数据中 key相同的merge_key的值
- 安卓---------Edditview
- android 自定属性的使用
- 相随心转,枯荣立现
- C程序设计Week11作业
- C#中常用的几种读取XML文件的方法
- memcached命令行参数说明
- 第六讲:使用Store查看邮件(1)
- Oracle 索引的维护
- 请用html5标准完成以下页面布局(要求完整书写整个页面的html代码,不需要写CSS)
- 求交通仿真程序