Android重写View并且自定义属性(二)
2019独角兽企业重金招聘Python工程师标准>>>
通过自定义属性 实现如下效果:
第一步:在res\values的目录下新建一个文件attrs.xml
声明一些自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustomViewStyle"><attr name="customText" format="string" /><attr name="customTextColor" format="color" /><attr name="customTextSize" format="dimension" /></declare-styleable></resources>
第二步:在layout目录下新建布局文件activity_main.xml
特别注意要在外层控件加上这个声明:
格式:xmlns:(你自定义名称)="http://schemas.android.com/apk/(你应用的包名)"
xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"
或者
xmlns:xr="http://schemas.android.com/apk/res-auto"
推荐使用第二种
在布局文件中加入这些自定义的属性:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/black"android:orientation="vertical" ><com.rong.activity.CustomViewandroid:layout_width="300dp"android:layout_height="300dp"android:layout_centerInParent="true"android:background="#ff0000"xr:customText="自定义控件"xr:customTextColor="#000000"xr:customTextSize="40sp" /></RelativeLayout>
第三部继承View重写
package com.rong.activity;import com.rong.test.R;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;/*** 自定义控件* * @author 徐荣**/
public class CustomView extends View {/*** 自定义画笔*/private Paint mPaint;/*** 文字范围*/private Rect mBounds;/*** 自定义文字*/private String customText;/*** 自定义大小*/private int customTextSize;/*** 自定义颜色*/private int customTextColor;public CustomView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewStyle);// 获取自定义文字customText = typedArray.getString(R.styleable.CustomViewStyle_customText);// 获取自定义文字大小customTextSize = typedArray.getDimensionPixelSize(R.styleable.CustomViewStyle_customTextSize, 28);// 或者自定义文字颜色customTextColor = typedArray.getColor(R.styleable.CustomViewStyle_customTextColor, Color.WHITE);// 要回收这个typedArray对象typedArray.recycle();initView();}public void initView() {// 初始化画笔mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setStyle(Paint.Style.FILL);mPaint.setColor(customTextColor);mPaint.setTextSize(customTextSize);// 生成文字区域mBounds = new Rect();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// 获取文字显示区域mBoundsmPaint.getTextBounds(customText, 0, customText.length(), mBounds);//使文字宽居中显示=控件的宽度/2 -文字的宽度/2float helfWidth = getWidth() / 2 - mBounds.width() / 2;//使文字高居中显示=控件的宽度/2 +文字的宽度/2float helfHeight = getHeight() / 2+mBounds.height()/2;//绘制文字canvas.drawText(customText, helfWidth, helfHeight, mPaint);}}
Run
转载于:https://my.oschina.net/547217475/blog/604325
Android重写View并且自定义属性(二)相关推荐
- Android 自定义View 三板斧之二——组合现有控件
通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 上文说过了如何继承现 ...
- Android 自定义View,自定义属性--自定义圆形进度条(整理)
很多的时候,系统自带的View满足不了我们的功能需求,那么我们就需要自定义View来满足我们的需求 自定义View时要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为 ...
- 精通Android自定义View(五)自定义属性值使用详情
1 可查看Android自定义View的基本使用 1 精通Android自定义View(一)自定义控的基本使用 2 精通Android自定义View(二)自定义属性使用详解 2 string 字符串 ...
- 精通Android自定义View(四)自定义属性使用详解
1.简述 对于自定义属性,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleable和item等标签 ...
- Android自定义View初探(二)——仿360垃圾清理
明天就是五一劳动节了,在这里先祝各位程序猿劳动节快乐,别在加班了! 自从尝试过写自定义View(Android自定义View初探(一)--饼图)之后,每当看到别人的应用时,总是在想别人的实现方式,或许 ...
- Android中View绘制优化二一---- 使用include /标签复用布局文件
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译二: 使用<include />标签复用布局文件 翻译地址:http://developer. ...
- android自定义View学习(二)----自定义绘图
自定义绘图 自定义视图中最重要的部分是它的外观.根据您的应用需求,自定义绘图可以很容易或复杂.本篇涵盖了一些最常见的操作 onDraw() 绘制自定义视图中最重要的步骤是重写该onDraw()方法.参 ...
- Android自定义View 之自定义属性
1 自定义属性值 自定义view的起步是自定义属性,并且正确的读取属性. 在res/values/attrs.xml的文件中创建属性: <declare-styleable name=" ...
- 精通Android自定义View(十二)绘制圆形进度条
1 绘图基础简析 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制 ...
最新文章
- 算法科普:神秘的 DES 加密算法
- 编译参数-ObjC的说明
- 理论实践都重要,交换机安装全接触
- 进击的Android Hook 注入术《一》
- oracle 在数据库打开状态下进行备份时_下面描述不正确的是,Oracle数据库DBA面试题50道及答案_经典...
- C#经典名著:《C#入门经典》(第4版)
- Zookeeper的api的简单使用(转载)
- keepalived实现应用高可用
- html 自定义标签的作用
- RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较(转)
- 一个不错的linux学习资料下载的网址
- Cadence Allegro如何复用设计参数?
- [【震撼】珠海中学曝【师生课堂互殴门】]
- Oracle 10g 的后台进程
- 获取wifi 的ssid出现unknown ssid
- 百思不得姐(4.5.6)最新版高仿
- 数据库应用系统的四个层次划分
- 华为路由器接口如何区分_华为路由的线路输出的两种不同方法简介
- Linux C++ IDEs
- [014量化交易] python 通过tushare 获取股票数据、名称、股票代码、指定股票名
热门文章
- ASP.NET 常用的33种代码(转,收藏一下,以备后查)
- vector 注意事项
- AndroidStudio安卓原生开发_android中使用StringUtils类_引入commons-lang的jar---Android原生开发工作笔记132
- AndroidStudio_Android使用OkHttp发起Http请求_以及使用时需要注意的点---Android原生开发工作笔记220
- Spring Security OAuth2.0_总结_Spring Security OAuth2.0认证授权---springcloud工作笔记157
- STM32工作笔记0092---CAN通信基础知识介绍-M3
- HTML5学习笔记---Html5简单理解,发展情况...
- 【单片机基础篇】舵机模块使用
- linux驱动编写(声卡驱动之asoc移植)
- python编程(基于订阅模式的mvc实现)