Android自定义View 自定义xml属性设置
来源:互联网 发布:高频注入源码 编辑:程序博客网 时间:2024/06/10 16:47
Android自定义View实现很简单
继承View,重写构造函数、onDraw,(onMeasure)等函数。
如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。
在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/Android项目所在的包路径即R文件所在的包(注意:不是自定义view包路径)". 配置好后按Alt加 / 键在使用自定义属性的时候没提示自定义属性代码,重启eclipse可以解决,本人亲测。或者写成:xmlns:my="http://schemas.android.com/apk/res-auto"不过我觉得这个靠谱多了》
在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。
实例:
- package demo.view.my;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Paint.Style;
- import android.util.AttributeSet;
- import android.view.View;
- /**
- * 这个是自定义的TextView.
- * 至少需要重载构造方法和onDraw方法
- * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
- * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
- * 并根据需要设定默认值,放在在xml文件中没有定义。
- * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
- * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
- * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包
- * @author Administrator
- *
- */
- public class MyView extends View {
- Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息
- public MyView(Context context) {
- super(context);
- }
- public MyView(Context context, AttributeSet attrs){
- super(context, attrs);
- mPaint = new Paint();
- //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
- //在使用完成后,一定要调用recycle方法
- //属性的名称是styleable中的名称+“_”+属性名称
- TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
- int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定
- float textSize = array.getDimension(R.styleable.MyView_textSize, 36);
- mPaint.setColor(textColor);
- mPaint.setTextSize(textSize);
- array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
- }
- public void onDraw(Canvas canvas){
- super.onDraw(canvas);
- //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形
- //mPaint = new Paint();
- //mPaint.setColor(Color.RED);
- mPaint.setStyle(Style.FILL); //设置填充
- canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形
- mPaint.setColor(Color.BLUE);
- canvas.drawText("我是被画出来的", 10, 120, mPaint);
- }
- }
相应的属性文件:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyView">
- <attr name="textColor" format="color"/>
- <attr name="textSize" format="dimension"/>
- </declare-styleable>
- </resources>
在布局文件中的使用:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <demo.view.my.MyView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- my:textColor="#FFFFFFFF"
- my:textSize="22dp"
- />
- </LinearLayout>
0 0
- Android自定义View 自定义xml属性设置
- Android 自定义view设置xml属性
- android 自定义view添加自定义xml属性
- android在自定义View的xml中设置自定义的成员属性
- Android自定义View读取XML属性详解
- Android笔记之View XML属性自定义
- android中自定义View设置属性
- Android自定义View的自定义属性atrrs.xml解析
- Android自定义view自定义属性
- Android自定义View-自定义属性
- Android自定义View-自定义属性
- 【Android】如何在自定义View中设置自定义的属性
- 自定义Android View属性
- Android自定义 view属性
- android 自定义view属性
- android 自定义view属性
- android View自定义属性
- Android 自定义控件xml设置属性
- xib中多个cell下的Autoresizing问题
- Python学习笔记之dict、set
- Valid Parentheses
- 第十七天:poj1019(纯属数学题目,找到规律就能解出)
- 快速幂或矩阵快速幂
- Android自定义View 自定义xml属性设置
- 计算机图形学
- 复数模板类2
- COGS-363-土地购买-斜率优化
- leetcode 日经贴,Cpp code -Partition List
- In exchange balloon with us!
- STM 32关于USART的使用笔记
- [Unity实战]剧情对话(二)
- 关于自带地图导航