练习,自定义TextView(1.1)
重新自定义TextView是非常有趣的事情,跟着Android4高级编程,通过自定义TextView,来敲一下代码:

这个是那么的简单,自定义TextView,新建CustomTextView继承TextView
public class CustomTextView extends TextView {
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
private void init() {
//获得对资源表的引用
Resources myResources=getResources();
//创建将在onDraw方法中使用的画刷
marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(myResources.getColor(R.color.noted_margin));
linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(myResources.getColor(R.color.noted_lines));
//获得页面背景色和边缘宽度
paperColor=myResources.getColor(R.color.noted_paper);
margin=myResources.getDimension(R.dimen.noted_margin);
}
@Override
protected void onDraw(Canvas canvas) {
//绘制页面的颜色
canvas.drawColor(paperColor);
//绘制边缘
canvas.drawLine(0, getMeasuredHeight(),getMeasuredWidth(), getMeasuredHeight(), linePaint);
//draw margin
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
//移动文本,让它跨过边缘
canvas.save();
canvas.translate(margin, 0);
//使用TextView渲染文本
super.onDraw(canvas);
canvas.restore();
}
}
xml:
<com.example.customtextviewbychen.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
it is very important to learn the knowledge about “onDraw”.
练习,自定义TextView(1.1)的更多相关文章
- 自定义TextView 调用ttf格式字体
自定义TextView 调用ttf格式字体 1.<strong>将ttf格式文件存放在assets/fonts/下</strong> 注:PC系统字体存放在C:\Windows ...
- [原创]Android秒杀倒计时自定义TextView
自定义TextView控件TimeTextView代码: import android.content.Context; import android.content.res.TypedArray; ...
- ios开发之自定义textView
自定义textView,从理论上讲很简单,根据需求自定义,比如我在开发中的需求就是现实一个字数的限制以及根据输入的文字改变提示剩余字数,那么开始我的基本思路就是自定义一个View,而里面包含一个子控件 ...
- 安卓自定义TextView实现自动滚动
xml文件代码 <com.mobile.APITest.ScrollEditText android:id="@+id/statusEditText" android:lay ...
- Android开发学习笔记-自定义TextView属性模版
如果项目中有很多个控件使用的是同一种样式,则为了方便,可以将样式设置到系统中去,这样使用的时候会方便很多. 下面是自定义样式模版的方法. 1.在style.xml文件中添加自己要设置的样式内容 < ...
- 自定义TextView带有各类.ttf字体的TextView
最近项目遇到了将普通文字转化为带有字体样式的文字,这里就涉及到了.ttf文件,我上网百度了不少资料最终终于实现了,现在想想其实并不复杂 1,你需要下载一种.ttf字体文件,你可以从网上找到一种字体的. ...
- Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果
一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...
- [置顶] android 自定义TextView
系统自带的控件TextView有时候没满一行就换行了,为了解决这个问题,自定义了一个TextView,只有一行显示不完全的情况下才会去换行显示,代码如下: package com.open.textv ...
- Android自定义View(一、初体验自定义TextView)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客] 目录: 继承View重写onDraw方法 自 ...
随机推荐
- Swift 4.0 数组(Array)学习
定义数组常量(常量只有读操作) let array1: [Int] = [11, 55, 5] let array2 = [11, 55, 5] 定义数组变量 var array: [Int] = [ ...
- Python-文件操作-之优化购物车
#此次购物车优化,主要使用了文件操作的相关方法,有买家入口,和商家入口 一.买家入口 1.买家第一次启动程序输入金额,金额会记录到文件里,再登录就读取文件里保存的金额,买家可以购买商品,按 ‘q’ 退 ...
- 《前端之路》- TypeScript (四) class 中各类属性、方法,抽象类、多态
目录 一.TypeScript 中的类 二.TypeScript 中类的继承 三.TypeScript 中公共,私有与受保护的修饰符 3-1.属性的 public 3-2.属性的 private 3- ...
- 贪心-最大相容区间-Maximum Number of Events That Can Be Attended
2020-02-16 16:24:19 问题描述: 问题求解: 看起来就像是sort + 贪心,但是具体如何做呢? 实际上本题是最大相容区间的变种题,在最大相容区间里,我们按照结束时间对interva ...
- 解决使用requests_html模块,req.html.render()下载chromium速度慢问题
1.第一步,代码如下: from requests_html import HTMLSession url="https://www.baidu.com/" headers={ & ...
- HDU - 3068 最长回文manacher马拉车算法
# a # b # b # a # 当我们遇到回判断最长回文字符串问题的时候,若果用暴力的方法来做,就是在字符串中间添加 #,然后遍历每一个字符,找到最长的回文字符串.那么马拉车算法就是在这个基础上进 ...
- python接口调用把执行结果追加到测试用例中
python操作excel的三个工具包如下,注意,只能操作.xls,不能操作.xlsx. xlrd: 对excel进行读相关操作 xlwt: 对excel进行写相关操作 xlutils: 对excel ...
- 通过pip控制台查看已安装第三方库版本及最新版本
首先执行[pip --help]查看pip命令: 由Commands知:[pip list]命令查看已安装第三方库,另[pip list --outdated]可查看有新版本的第三方库.
- 【tensorflow2.0】处理结构化数据-titanic生存预测
1.准备数据 import numpy as np import pandas as pd import matplotlib.pyplot as plt import tensorflow as t ...
- 改进ls的实现
一.要求 参见附图,改进你的ls的实现.提交代码运行截图和码云链接 二.步骤 目录 ls 功能:列出目录内容,显示文件信息 ls -l:显示当前工作目录下包含目录及属性详细信息(共7列) 第一列:文件 ...