1、自定义View的属性

2、在View的构造方法中获得我们自定义的属性

3、重写onMesure

4、重写onDraw

3这个步骤不是必须,当然了大部分情况下还是需要重写的。

1、自定义View的属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。

<?xml version="1.0" encoding="utf-8"?>
<resources><attr name="txtName" format="string"/><attr name="txtColor" format="color"/><attr name="txtSize" format="dimension" /><declare-styleable name="titleStyle"><attr name="txtName"/><attr name="txtColor"/><attr name="txtSize"/></declare-styleable>
</resources>

定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:

一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;

编写的时候工具会提醒你使用哪种,不知道也可以Google搜索下

接下来就自定义View

public class CustomTitleView extends View{private  String txtName;private int txtColor,txtSize;private  Paint mPaint;private Rect mBounds;public CustomTitleView(Context context) {this(context, null);}public CustomTitleView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {//具体操作}
}

定义完自定义的View ,就该调用我们自定义的View了。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:title="http://schemas.android.com/apk/res/com.example.androidDemo"  《》android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><com.example.androidDemo.View.CustomTitleViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:padding="5dp"title:txtName="你好"title:txtColor="#ffffff"title:txtSize="16sp"/>
</RelativeLayout>

注意代码中的这行,自定义命名空间,com.example.androidDemo是项目包路径

xmlns:title="http://schemas.android.com/apk/res/com.example.androidDemo"

使用自定义命名空间:

        title:txtName="你好"title:txtColor="#ffffff"title:txtSize="16sp"

在View的构造方法中,获得我们的自定义的样式

public class CustomTitleView extends View{private  String txtName;private int txtColor,txtSize;private  Paint mPaint;private Rect mBounds;public CustomTitleView(Context context) {this(context, null);}public CustomTitleView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,R.styleable.titleStyle,defStyleAttr,0);int n = typedArray.getIndexCount();for (int i = 0; i < n; i++){int attr = typedArray.getIndex(i);switch (attr){case 0:txtName = typedArray.getString(attr);break;case 1:txtColor = typedArray.getColor(attr, Color.BLACK);break;case 2:txtSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));break;}}typedArray.recycle();/*** 获得绘制文本的宽和高 */mPaint = new Paint();mPaint.setTextSize(txtSize);// mPaint.setColor(mTitleTextColor);mBounds = new Rect();mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int width;int height ;if (widthMode == MeasureSpec.EXACTLY){width = widthSize;} else{mPaint.setTextSize(txtSize);mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);float textWidth = mBounds.width();int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());width = desired;}if (heightMode == MeasureSpec.EXACTLY){height = heightSize;} else{mPaint.setTextSize(txtSize);mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);float textHeight = mBounds.height();int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());height = desired;}setMeasuredDimension(width, height);}@Overrideprotected void onDraw(Canvas canvas) {mPaint.setColor(Color.YELLOW);canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);mPaint.setColor(txtColor);canvas.drawText(txtName, getWidth() / 2 - mBounds.width() / 2, getHeight() / 2 + mBounds.height() / 2, mPaint);}
}

其中

MeasureSpec.EXACTLY判断你传人的宽,高是不是精确赋值

android:layout_width="wrap_content"
 android:layout_height="wrap_content"

如果是wrap_content,

 mPaint.setTextSize(txtSize);mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);float textWidth = mBounds.width();int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());width = desired;

如果是200dp这类精确的宽高值,

if (widthMode == MeasureSpec.EXACTLY){width = widthSize;} 

效果图 ,是不是很好用呢

【android自定义控件】自定义View属性相关推荐

  1. android自定义控件是一个 内部类 如何在xml中引用,android 自定义view属性

    android 自定义view属性 一个完美的自定义控件也可以添加xml来配置属性和风格.要实现这一点,可按照下列步骤来做: 1) 添加自定义属性到xml文件中 2) 在xml的中,指定属性的值 3) ...

  2. Android 系统(264)---android进阶——自定义View

    android进阶--自定义View 软件架构 01.自定义View简介 - onMeasure,onDraw,自定义属性  https://www.jianshu.com/p/48944aad200 ...

  3. android五子棋编程教程全集,android简单自定义View实现五子棋

    本文实例为大家分享了android自定义View实现五子棋的具体代码,供大家参考,具体内容如下 先说一下吧,android的自定义View就是自己实现一个类去继承View,实现其中的方法,这里面我最感 ...

  4. android开发自定义View(三)仿芝麻信用积分

    此文参考了https://github.com/HotBitmapGG/CreditSesameRingView 感谢作者的分享!! 首先看一下支付宝上显示的样子 然后看一下模仿的效果 代码 基础部分 ...

  5. android 动态画直线,Android使用自定义view在指定时间内匀速画一条直线的实例代码...

    本文讲述了Android使用自定义view在指定时间内匀速画一条直线的实例代码.分享给大家供大家参考,具体如下: 1.效果图: 2.自定义view实现 public class UniformLine ...

  6. android 动态生成直线,Android使用自定义view在指定时间内匀速画一条直线的实例代码...

    本文讲述了Android使用自定义view在指定时间内匀速画一条直线的实例代码.分享给大家供大家参考,具体如下: 1.效果图: 2.自定义view实现 public class UniformLine ...

  7. Android 中自定义View 裁剪扇形图片

    Android 中自定义View 裁剪扇形图片 当需要裁剪图片为扇形区域时,使用Canvas.clipPath(path)方法可以裁剪为扇形区域 ps:此方法会导致绘制图片边缘有锯齿,暂无解决方法(知 ...

  8. 【Android】自定义View、画家(画布)Canvas与画笔Paint的应用——画图、涂鸦板app的实现

    利用一个简单的画图app来说明安卓的图形处理类与自定义View的应用. 如下图,有一个供用户自己任意画图.涂鸦的app, 这里不做那么花俏了,仅提供黑白两色,但可以改变笔尖的粗细. 实质上这里的橡皮擦 ...

  9. android自定义组件属性,android自定义控件并添加属性的方法以及示例

    安卓系统为我们提供了丰富的控件,但是在实际项目中我们仍然需要重新通过布局来实现一些效果,比如我们需要一个上面图标,下面文字的button,类似于下面这样的: 最直接的解决办法是通过将imageview ...

  10. android自定义控件中文乱码,Android笔记--自定义View之组合控件

    Android-自定义View 分享是最好的记忆-- 如需转发请注明出处 [强调]:共同学习 共同进步 不喜勿喷 内容简介 前言 实现 总结 1. 前言 这次更新有2个目的 1. 复用控件,而不是每次 ...

最新文章

  1. OpenStack虚机网卡的创建过程
  2. .NET平台BigO算法复杂度备忘
  3. 大数据应用项目创新大赛_温州首届大数据应用创新大赛决赛名单公布!有你的单位吗?...
  4. 有用的网址集合, IT杂谈
  5. 『设计模式』80年代的人们就已经领悟了设计模式-- 发布者/订阅者模式 (包括发布者/订阅者模式和观察者模式的区别)
  6. linux shell之替换目录下包含关键字所有文本里面的内容
  7. Spark常见问题解决办法
  8. 惠普打印机只打印一半_惠普打印机如何安装 惠普打印机加墨方法【介绍】
  9. c java json_cJSON_json包的C语言解析库
  10. 英语每日阅读---1、科学美国人60秒:如果觉得唱歌很难 那就吹口哨吧
  11. .NET Mail : 注意Win 7 不再包含SMTP服务
  12. c语言里,关于宏定义的使用
  13. 乡村少年宫计算机室活动方案,社区乡村农村少年宫活动方案少儿培训开展情况...
  14. 【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
  15. 基于Android的本地电子书阅读器的设计与实现Ebook(2)
  16. learning scala type alise
  17. linux7.1装宝塔,CentOS 7.6 宝塔 + SSRPanel 安装
  18. 2020年国赛A题目思路(高教杯全国大学生数学建模竞赛)
  19. 路由器重温——串行链路链路层协议积累-帧中继FR协议
  20. 《四叶游戏》:梦想执念·棒球1号位

热门文章

  1. 【POJ3784】【对顶堆 — 动态维护中位数】Running Media
  2. sap未分摊差异怎么处理_聊一聊,临时外包员工差异化薪酬要怎么处理
  3. 5年级用计算机器探索规律,《小数除法》用计算器探索规律
  4. 搜索结果去重_华为诺亚方舟实验室推荐与搜索方向六项研究成果获CIKM 2020录用...
  5. android ImageView加圆角
  6. Linux下Mongodb安装和启动配置
  7. 20145235李涛《网络对抗》Exp8 Web基础
  8. 替换换行符:回车换行CR/LF
  9. iOS 关于本地持久化存储的探讨
  10. ADO.NET数据访问方式:SqlDataReader