重新自定义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)的更多相关文章

  1. 自定义TextView 调用ttf格式字体

    自定义TextView 调用ttf格式字体 1.<strong>将ttf格式文件存放在assets/fonts/下</strong> 注:PC系统字体存放在C:\Windows ...

  2. [原创]Android秒杀倒计时自定义TextView

    自定义TextView控件TimeTextView代码: import android.content.Context; import android.content.res.TypedArray; ...

  3. ios开发之自定义textView

    自定义textView,从理论上讲很简单,根据需求自定义,比如我在开发中的需求就是现实一个字数的限制以及根据输入的文字改变提示剩余字数,那么开始我的基本思路就是自定义一个View,而里面包含一个子控件 ...

  4. 安卓自定义TextView实现自动滚动

    xml文件代码 <com.mobile.APITest.ScrollEditText android:id="@+id/statusEditText" android:lay ...

  5. Android开发学习笔记-自定义TextView属性模版

    如果项目中有很多个控件使用的是同一种样式,则为了方便,可以将样式设置到系统中去,这样使用的时候会方便很多. 下面是自定义样式模版的方法. 1.在style.xml文件中添加自己要设置的样式内容 < ...

  6. 自定义TextView带有各类.ttf字体的TextView

    最近项目遇到了将普通文字转化为带有字体样式的文字,这里就涉及到了.ttf文件,我上网百度了不少资料最终终于实现了,现在想想其实并不复杂 1,你需要下载一种.ttf字体文件,你可以从网上找到一种字体的. ...

  7. Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果

    一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...

  8. [置顶] android 自定义TextView

    系统自带的控件TextView有时候没满一行就换行了,为了解决这个问题,自定义了一个TextView,只有一行显示不完全的情况下才会去换行显示,代码如下: package com.open.textv ...

  9. Android自定义View(一、初体验自定义TextView)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客] 目录: 继承View重写onDraw方法 自 ...

随机推荐

  1. iOS 指纹认证登陆开发(TouchID)

    设计思路 TouchID 关联账号 用户登陆成功 -> 开启TouchID登陆 -> TouchID验证 -> 记录用户信息(验证通过) -> 关联完成 TouchID 登陆 ...

  2. Java基础 - 数据类型和运算符

    Java 语言支持的类型分为两类:基本数据类型(Primitive Type)和引用类型(Reference Type). 目录 基本数据类型 数值类型 整数类型 byte short int lon ...

  3. Github搜索技巧整理

    Github官方网址:https://github.com/ 一.详细官方文档:https://help.github.com/en/github/searching-for-information- ...

  4. Trie 字典树,hdu1251

    参考博客:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 字典树就是单词树,顺着一条路径到达终止结点就形成一个单词,该单词的前缀包含在这 ...

  5. NLP interview

    2019-08-26 17:19:58 1)聊实习项目 2)代码题,二维数组中的查找某个target 3)讲一些最能体现创新能力的工作,而不是一些工程上的实现 4)讲论文可以从哪些方面做创新点,文本生 ...

  6. 向Hive中传入变量的方式

    Hive向程序中传递变量的方式 暴力替换 字符串替换 正则替换 模板引擎 系统环境变量 shell环境变量:${env:varname} system系统变量:${system:varname} hi ...

  7. Windows下命令行MySQL安装

    通过zip压缩包文件直接安装 1.下载链接 https://dev.mysql.com/downloads/mysql/ 下载好后解压移动文件夹 2.配环境变量 path路径追加 3.创建初始化文件 ...

  8. ehcahe + redis 实现多级缓存

    1,了解数据存储的位置的不同 数据库:存储在磁盘上 redis:存储在内存上 ehcache:应用内缓存 缓存的目的:是为了将数据从一个较慢的介质上读取出来,放到一个较快的介质上,为了下次读取的时候更 ...

  9. 第 k 小的数

    一.寻找两个有序数组的中位数 1.1 问题描述 给定两个大小为 m 和 n 的不同时为空的有序数组 nums1 和 nums2.找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m ...

  10. iOS 性能优化点

    用 ARC 管理内存 在正确的地方使用 reuseIdentifier 尽量把 views 设置为完全不透明 如果你有透明的 Views 你应该设置它们的 opaque(不透明)属性为 YES.例如一 ...