android在布局文件中自定义参数并在初始化时获取
2016-06-01 16:40
435 查看
在一些自定义view中我们往需要预先定义一些默认属性,这些参数常常跟view绑定的,因而在定义view的布局文件中添加这些属性就显得尤为有价值。整个过程分三步:
1.定义declare-styleable,用于TypedArray来绑定对于的参数,在此需要在value目录里面新建文件:attrs.xml
Java代码
<resources> 点击打开链接
<declare-styleable name="Def">
<!-- The first screen the workspace should display. -->
<attr name="num" format="integer" />
<attr name="length" format="integer" />
</declare-styleable>
</resources>
2.布局文件中配置控件,这里包含xml声明,还有对应的属性值
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<span style="color: #ff0000;">xmlns:test="http://schemas.android.com/apk/res/com.yuhua.test"</span>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<span style="color: #ff0000;"> <com.yuhua.test.LocalView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
test:num ="3"
test:length="5"
/></span>
</LinearLayout>
3. 通过映射获取配置的属性
Java代码
public class LocalView extends View {
private static final String TAG = "LocalView";
public LocalView(Context context) {
super(context);
}
public LocalView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public LocalView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Def);
Log.i(TAG, a.getInt(R.styleable.Def_length, 0) + "===" + a.getInt(R.styleable.Def_num, 0));
}
}
转自:http://geyubin.iteye.com/blog/1562868
1.定义declare-styleable,用于TypedArray来绑定对于的参数,在此需要在value目录里面新建文件:attrs.xml
Java代码
<resources> 点击打开链接
<declare-styleable name="Def">
<!-- The first screen the workspace should display. -->
<attr name="num" format="integer" />
<attr name="length" format="integer" />
</declare-styleable>
</resources>
2.布局文件中配置控件,这里包含xml声明,还有对应的属性值
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<span style="color: #ff0000;">xmlns:test="http://schemas.android.com/apk/res/com.yuhua.test"</span>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<span style="color: #ff0000;"> <com.yuhua.test.LocalView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
test:num ="3"
test:length="5"
/></span>
</LinearLayout>
3. 通过映射获取配置的属性
Java代码
public class LocalView extends View {
private static final String TAG = "LocalView";
public LocalView(Context context) {
super(context);
}
public LocalView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public LocalView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Def);
Log.i(TAG, a.getInt(R.styleable.Def_length, 0) + "===" + a.getInt(R.styleable.Def_num, 0));
}
}
转自:http://geyubin.iteye.com/blog/1562868
相关文章推荐
- ajax中data传参的两种方式分析
- 一个通过script自定义属性传递配置参数的方法
- javascript实现浏览器窗口传递参数的方法
- jquery mobile changepage的三种传参方法介绍
- C#实现向多线程传参的三种方式实例分析
- javascript event在FF和IE的兼容传参心得(绝对好用)
- jquery bind(click)传参让列表中每行绑定一个事件
- jsp地址栏传中文显示乱码解决方法分享
- C#实现.net页面之间传值传参方法汇总
- Shell脚本传参数方法总结
- js实现页面a向页面b传参的方法
- php使用$_POST或$_SESSION[]向js函数传参
- Android App中自定义View视图的实例教程
- Android中自定义View实现圆环等待及相关的音量调节效果
- php向js函数传参的几种方法
- js动态添加onclick事件可传参数与不传参数
- 用js通过url传参把数据从一个页面传到另一个页面
- Java中用户向系统传递参数的三种基本方式实例分享
- Android开发使用自定义view实现ListView下拉的视差特效功能
- 自定义图表控件--同时显示柱状图和折线图