自定义View(1)------------自定义属性

2024-06-17 16:58

本文主要是介绍自定义View(1)------------自定义属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先贴一个网址,这个大神画的不错  http://blog.csdn.net/wangchunlei123/article/details/50478913

自定义View的步骤:

1、创建一个自定义的View如:MyNoteView继承View或者View的子类

写构造方法:根据需要写出构造函数

public MyNoteView(Context context)   通过java代码写布局    new 这个对象的时候会执行该方法

public MyNoteView(Context context, AttributeSet attrs)    使用xml文件布局的时候使用  

参数说明:AttributeSet:引用资源的属性集合

public MyTextView(Context context, AttributeSet attrs, int defStyleAttr)

也是在xml文件中使用该方法 如果本类中 写了该构造方法,则必须还要添加 上面第二个构造方法

参数说明:defStyleAttr 默认的一个属性风格id

2、自定义属性:在res\values文件夹新建attrs.xml文件

使用declare-styleable标签声明自定义View,声明时anme的值必须与使用该属性的View 名称相同,如:

<declare-styleable name="MyTextView"><attr name="textColor" format="color|reference"></attr><attr name="textSize" format="dimension"></attr><attr name="text" format="string"></attr></declare-styleable>
(1)定义每个属性的 name,     name可以随便命名,但是最好见名知意
(2)   定义每个属性的format(格式)  填写格式类型可以多种       以   “|”  符号链接

format的类型

1、reference   参考某一资源 ,通常是@开头,例如@+id/xxxx,@id/xxx
2、color          颜色值
3、boolean     布尔值
4、dimension 尺寸值(带有单位的 sp/dp)
5、float           浮点型
6、integer 整形
7、string 字符串
8、fraction       百分比
9、enum 枚举
10、flag 位或运算

(3)在xml文件中使用自定义的属性,

注意:

①自定义View的引用 必须是包名+类名(全路径)

②根元素的中必须增加了一个额外的命名空间: xmlns:app="http://schemas.android.com/apk/res/com.phone.day28_customviewattr"      其中app表示      元素中使用以app开头的属性,

...../res/后表示完整的包名com.phone.day28_customviewattr    具体使用如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res/com.phone.day28_customviewattr"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><!-- 自定义View的引用 必须是包名+类名(全路径) --><com.phone.day28_customviewattr.MyTextViewandroid:id="@+id/myTextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"app:text="我是石头里蹦出来的"app:textColor="@android:color/holo_blue_light"app:textSize="24sp" /></RelativeLayout>
③在java代码中要使用自定义的属性

在 自定义View 的构造方法中  通过TypedArray 将我们设置的值拿出来,赋予画笔或画布进行绘制

我们调用Context.obtainStyledAttributes方法获得TypedArray的对象

在attrs.xml中定义的名称,通过R.styleable来访问,按照attrs.xml中定义的属性的类型,使用不同的get方法获取指定属性的值。

TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);color = array.getColor(R.styleable.MyTextView_textColor, Color.BLACK);text = array.getString(R.styleable.MyTextView_text);size = array.getDimension(R.styleable.MyTextView_textSize, 18);
完整获取属性代码如下:

public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub// 画笔mPaint = new Paint();// 帮助我们将设置的属性资源拿到这里TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);color = array.getColor(R.styleable.MyTextView_textColor, Color.BLACK);text = array.getString(R.styleable.MyTextView_text);size = array.getDimension(R.styleable.MyTextView_textSize, 18);mPaint.setColor(color);mPaint.setTextSize(size);// 回收array.recycle();}
(4)根据需要重写以下方法;

①测量,计算当前view 的大小   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
②位置 1自身相对于 父布局的位置 2如果自身是ViewGroup 还有 childView 相对于自身的位置

protected void onLayout(boolean changed, int left, int top, int right,  int bottom)

③控制View在屏幕上的渲染效果,protected void onDraw(Canvas canvas)

④监听手势滑动public boolean onTouchEvent(MotionEvent event)

以下写一个简单的例子并贴出一个完整代码:

自定义的一个View   MyTextView

package com.phone.day28_customviewattr;import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;public class MyTextView extends View {Paint mPaint;int color;float size;String text = "";/*** * 构造方法 一般只使用第一个(new 的时候)和 第二个xml 文件中* * @param context*/// 在java代码中 new 这个对象的时候会执行该方法public MyTextView(Context context) {super(context);}// 当在xml文件中使用的时候会执行该方法// AttributeSet:引用资源的属性集合@SuppressLint("Recycle")public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub// 画笔mPaint = new Paint();// 帮助我们将设置的属性资源拿到这里TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);color = array.getColor(R.styleable.MyTextView_textColor, Color.BLACK);text = array.getString(R.styleable.MyTextView_text);size = array.getDimension(R.styleable.MyTextView_textSize, 18);mPaint.setColor(color);mPaint.setTextSize(size);// 回收array.recycle();}// //也是在xml文件中使用该方法 如果本类中 写了该构造方法,则必须还要添加 上面第二个构造方法// defStyleAttr 默认的一个属性风格idpublic MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/*** 测量,计算当前view 的大小*/@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}/*** 位置 1自身相对于 父布局的位置 2如果自身是ViewGroup 还有 childView 相对于自身的位置* */@Overrideprotected void onLayout(boolean changed, int left, int top, int right,int bottom) {super.onLayout(changed, left, top, right, bottom);}/*** 绘制 1 画布 2 画笔 3 内容* */@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// 绘制  当前textViewcanvas.drawText(text, 100, 100, mPaint);}/*** 监听手势滑动*/@Overridepublic boolean onTouchEvent(MotionEvent event) {return super.onTouchEvent(event);}}
atrrs.xml文件的代码

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="MyTextView"><attr name="textColor" format="color|reference"></attr><attr name="textSize" format="dimension"></attr><attr name="text" format="string"></attr></declare-styleable>
</resources>
使用自定义属性的xml文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res/com.phone.day28_customviewattr"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><!-- 自定义View的引用 必须是包名+类名(全路径) --><com.phone.day28_customviewattr.MyTextViewandroid:id="@+id/myTextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"app:text="我是石头里蹦出来的"app:textColor="@android:color/holo_blue_light"app:textSize="24sp" /></RelativeLayout>
在自定义View中重写相应的方法就可以了


关于自定义View 的其他知识会在之后写出来


这篇关于自定义View(1)------------自定义属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1070020

相关文章

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字