博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
界面无小事(五):自定义TextView
阅读量:6880 次
发布时间:2019-06-27

本文共 3295 字,大约阅读时间需要 10 分钟。


目录

  • 效果图
  • 前言
  • 自定义属性
  • MeasureSpec类
  • 颜色解析
  • 字号转换
  • 最后

效果图

不多废话, 直接上图, 如果感兴趣再看下去.


前言

写第四篇滚动选择器的时候, 在自定义视图这里含糊了, 有些地方没说清楚, 这次补上关于自定义视图的部分.


自定义属性

自定义视图的一个要点就是添加自定义属性. 这里我们填上三个常用的, 文本, 颜色, 字号. 然后在布局文件中就可以使用了. 最后在自定义类中获取属性并赋值.

复制代码
xmlns:app="http://schemas.android.com/apk/res-auto"复制代码
复制代码
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    init(context, attrs);}private void init(Context context, AttributeSet attrs) {    // 获取自定义属性    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);    mSize = ta.getDimension(R.styleable.MyTextView_size, 16);    mText = ta.getString(R.styleable.MyTextView_text);    mColor = ta.getColor(R.styleable.MyTextView_color, Color.BLACK);    ta.recycle();    // 设置画笔    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    mPaint.setTextSize(mSize);    // 设置背景颜色    mBkColor = Color.BLUE;}复制代码

MeasureSpec类

关于onMeasure方法, 最重要的就是就是MeasureSpec类的使用了. 其实主要也就是要算好match_parentwrap_content. match_parent和具体数值都是EXACTLY. wrap_content是AT_MOST. ScrollView或者是ListView就会是UNSPECIFIED.

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    int width = 0;    int height = 0;    int specMode = MeasureSpec.getMode(widthMeasureSpec);    int specSize = MeasureSpec.getSize(widthMeasureSpec);    switch (specMode) {        case MeasureSpec.EXACTLY:            width = getPaddingLeft() + getPaddingRight() + specSize;            break;        case MeasureSpec.AT_MOST:        case MeasureSpec.UNSPECIFIED:            width = (int) (getPaddingLeft() + getPaddingRight()                    + mPaint.measureText(mText));            break;    }    specMode = MeasureSpec.getMode(heightMeasureSpec);    specSize = MeasureSpec.getSize(heightMeasureSpec);    switch (specMode) {        case MeasureSpec.EXACTLY:            height = getPaddingTop() + getPaddingBottom() + specSize;            break;        case MeasureSpec.AT_MOST:        case MeasureSpec.UNSPECIFIED:            Paint.FontMetrics fmi = mPaint.getFontMetrics();            float textHeight = Math.abs(fmi.bottom - fmi.top);            height = (int) (getPaddingTop() + getPaddingBottom() + textHeight);            break;    }    setMeasuredDimension(width, height);}复制代码

有两个要点, 就是算字符串的宽度和高度, 宽度用Paint实例的measureText方法即可. 高度涉及到我在第四篇写的Paint.FontMetrics类, 就是用底部减去顶部取绝对值.


颜色解析

Color是个要处理的类, 当你用getColor函数获取到函数, 它是一个int值, 如果我们需要重新在原有颜色基础上变化, 就需要解析这个int, 将它还原成RGB.

/** * 依据颜色值获取rgb值 * * @param color 颜色值 * @return rgb值 */public int[] setColor(int color) {    int[] rgb = new int[3];    rgb[0] = (color & 0x00ff0000) >> 16;    rgb[1] = (color & 0x0000ff00) >> 8;    rgb[2] = (color & 0x000000ff);    return rgb;}复制代码

字号转换

要处理好字号问题, 最重要的就是转换, 代码中都是用px的, 但是布局文件一般用sp.

/** * sp转px */public static int sp2px(float spVal) {    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,            spVal, getContext().getResources().getDisplayMetrics());}/** * px转sp */public static float px2sp(float pxVal) {    return (pxVal / getContext().getResources().getDisplayMetrics().scaledDensity);}复制代码

最后

这样可以自定义一些简单的视图类了, 如果要更复杂的, 还需要去处理更多的参数, 特别是构造方法那个四参数的. 有意见或者建议评论区见, 喜欢记得点赞或者关注我哦~


转载地址:http://yufbl.baihongyu.com/

你可能感兴趣的文章
wordpress通过代码禁用IE8, IE9,IE10等IE浏览器兼容视图模式(Compatibility View)
查看>>
This application failed to start because it could not find or load the Qt platform plugin "windows"
查看>>
CSS3展现精彩的动画效果 css3的动画属性
查看>>
JSON+JSONP(加量不加价)
查看>>
windows下安装ubuntu,并用win引导ubuntu启动
查看>>
java开发常用工具
查看>>
在VMware Vcenter添加一块网卡后,启动虚机找不到网卡,发现有一个ens38(redhat7.5)...
查看>>
static 关键字和类的加载顺序
查看>>
安卓ListView基础应用
查看>>
【原创】PostgreSQL 快速创建空表TIPS
查看>>
利用PowerBI结合SCOM展示数据报表
查看>>
中学时代的记忆---老师的黑板
查看>>
Horizon View 6-View Connection Server部署⑴
查看>>
iptables 实战演练
查看>>
Python 学习笔记 - 线程(线程锁,信标,事件和条件)
查看>>
RHEL6基础四十一之selinux和iptables基础
查看>>
数据结构之单链表在第i个元素之前插入元素的算法
查看>>
Exchange Server 运维管理02:邮箱数据库存储原理
查看>>
Exchange Server2013 系列十:证书的配置
查看>>
Cygwin新手必读
查看>>