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

相关文章

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

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

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

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

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

Spring Boot 事务详解(事务传播行为、事务属性)

《SpringBoot事务详解(事务传播行为、事务属性)》SpringBoot提供了强大的事务管理功能,通过@Transactional注解可以方便地配置事务的传播行为和属性,本文将详细介绍Spr... 目录Spring Boot 事务详解引言声明式事务管理示例编程式事务管理示例事务传播行为1. REQUI