重新自定义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. MyBatis框架——逆向工程

    什么是逆向工程? 逆向工程师MyBatis提供的一种自动化配置方案,针对数据表自动生成MyBatis所需的各种资源,包括实体类.Mapper接口.Mapper.xml,但是逆向工程的缺陷在于只能针对单 ...

  2. jmeter3.3 接口压测入门和软件下载

    Jmeter3.3软件下载地址 https://download.csdn.net/download/qq_36625806/11076556 简单的使用教程 1.启动Jmeter 双击jemeter ...

  3. Manjaro更新后 搜狗拼音输入法突然无法正常使用

    之前Manjaro已经用了很久了,很多该配置的都已经配置好了,但是搜狗拼音在系统更新后突然无法使用 1检查 如下依赖 2.检查配置文件 3.发现一切配置没问题,此时输入 sogou-qimpanel ...

  4. css网页重置样式表(多版本)

    Eric reset.css html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, ...

  5. 不同label样本画图——颜色分配plt.cm.Spectral

    不同label样本画图——颜色分配plt.cm.Spectralhttps://blog.csdn.net/wang_zuel/article/details/102940092 关于plt.cm.S ...

  6. 一文看懂NLP神经网络发展历史中最重要的8个里程碑!

    导读:这篇文章中作者尝试将 15 年的自然语言处理技术发展史浓缩为 8 个高度相关的里程碑事件,不过它有些偏向于选择与当前比较流行的神经网络技术相关的方向.我们需要关注的是,本文中介绍的许多神经网络模 ...

  7. KDD 2019放榜!录取率仅14%,强调可重现性

    [导读]KDD 2019录取结果终于放榜了,今年Research和ADS两个 track共评审论文1900篇,其中Research track的录取率只有14%.今年也是KDD第一次采用双盲评审政策, ...

  8. C# 添加、修改、删除PPT中的超链接

    本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址.邮件地址.指定幻灯片等,此外,也可以参考文中编辑.删除幻灯片中已有超链接的方 ...

  9. war 和 war exploded

    IDEA 开发项目时,部署 tomcat 的 Deployment 选项出现: war 模式 可以称之为发布模式.先将 WEB 工程打成 war 包,然后再将其上传到服务器进行发布. war expl ...

  10. Google浏览器截取整个网页

    ~Ctrl+shift+i(开发者工具) ~Ctrl+shift+p ~输入full