android自定义控件(三) 自定义属性
2016-06-30 19:01
519 查看
书接上回
在xml里建立属性,然后java代码里用typedArray获得这些属性,得到属性后,利用属性做一些事.例:得到xml里的color,赋给paint.
1.在res/values/下新建attrs.xml
[html]
view plain
copy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView2">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
<!-- name="CustomView1"控件名称 得到TypedArray时用 -->
<!-- name="textColor" 对应test:textColor -->
<!-- format="color" 对应构造方法里a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF); -->
format详解可参照http://blog.csdn.net/ethan_xue/article/details/7315064
2.主要看构造函数
[java]
view plain
copy
public class CustomView2 extends View {
private Paint mPaint2;
private String mText = "drawText";
public CustomView2(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint2 = new Paint();
// TypedArray是存放资源的array,1.通过上下文得到这个数组,attrs是构造函数传进来的,对应attrs.xml
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView2);
// 获得xml里定义的属性,格式为 名称_属性名 后面是默认值
int textColor = a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF);
float textSize = a.getDimension(R.styleable.CustomView2_textSize, 35);
mPaint2.setColor(textColor);
mPaint2.setTextSize(textSize);
// 为了保持以后使用该属性一致性,返回一个绑定资源结束的信号给资源
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint2.setStyle(Style.FILL);
canvas.drawText(mText, 10, 60, mPaint2);
}
}
3.布局
[html]
view plain
copy
<?xml version="1.0" encoding="utf-8"?>
<!-- xmlns:test="http://schemas.android.com/apk/res/ethan.customview1" 包名 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/ethan.customview1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ethan.customview1.CustomView2
android:layout_width="wrap_content"
android:layout_height="wrap_content"
test:textColor="#f00"
test:textSize="20sp"
/>
</LinearLayout>
4.效果图
下载地址 http://download.csdn.net/detail/ethan_xue/4108832
在xml里建立属性,然后java代码里用typedArray获得这些属性,得到属性后,利用属性做一些事.例:得到xml里的color,赋给paint.
1.在res/values/下新建attrs.xml
[html]
view plain
copy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView2">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
<!-- name="CustomView1"控件名称 得到TypedArray时用 -->
<!-- name="textColor" 对应test:textColor -->
<!-- format="color" 对应构造方法里a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF); -->
format详解可参照http://blog.csdn.net/ethan_xue/article/details/7315064
2.主要看构造函数
[java]
view plain
copy
public class CustomView2 extends View {
private Paint mPaint2;
private String mText = "drawText";
public CustomView2(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint2 = new Paint();
// TypedArray是存放资源的array,1.通过上下文得到这个数组,attrs是构造函数传进来的,对应attrs.xml
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView2);
// 获得xml里定义的属性,格式为 名称_属性名 后面是默认值
int textColor = a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF);
float textSize = a.getDimension(R.styleable.CustomView2_textSize, 35);
mPaint2.setColor(textColor);
mPaint2.setTextSize(textSize);
// 为了保持以后使用该属性一致性,返回一个绑定资源结束的信号给资源
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint2.setStyle(Style.FILL);
canvas.drawText(mText, 10, 60, mPaint2);
}
}
3.布局
[html]
view plain
copy
<?xml version="1.0" encoding="utf-8"?>
<!-- xmlns:test="http://schemas.android.com/apk/res/ethan.customview1" 包名 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/ethan.customview1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ethan.customview1.CustomView2
android:layout_width="wrap_content"
android:layout_height="wrap_content"
test:textColor="#f00"
test:textSize="20sp"
/>
</LinearLayout>
4.效果图
下载地址 http://download.csdn.net/detail/ethan_xue/4108832
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories