自定义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

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python如何调用另一个类的方法和属性

《Python如何调用另一个类的方法和属性》在Python面向对象编程中,类与类之间的交互是非常常见的场景,本文将详细介绍在Python中一个类如何调用另一个类的方法和属性,大家可以根据需要进行选择... 目录一、前言二、基本调用方式通过实例化调用通过类继承调用三、高级调用方式通过组合方式调用通过类方法/静

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

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.