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

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

HTML5 data-*自定义数据属性的示例代码

《HTML5data-*自定义数据属性的示例代码》HTML5的自定义数据属性(data-*)提供了一种标准化的方法在HTML元素上存储额外信息,可以通过JavaScript访问、修改和在CSS中使用... 目录引言基本概念使用自定义数据属性1. 在 html 中定义2. 通过 JavaScript 访问3.

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效