自定义控件InfoTextView展示个人信息
来源:互联网 发布:怿不甚知书 编辑:程序博客网 时间:2024/05/23 12:53
像上面展示信息的控件,如果用传统的textview肯定是不够的,一般人都是用布局和控件去叠加,这样效率不高,而且代码量多。
/** * 如果用一般控件去堆砌看起来很麻烦 * 自定义infoTextView, * Created by Administrator on 2017-9-1. */public class InfoTextView extends TextView { public static final String TAG = "=test="; private TextPaint paint; private String rightText; private int intrinsicWidth; private float x; private float y; public InfoTextView(Context context) { this(context, null); } public InfoTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public InfoTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InfoTextView); rightText = typedArray.getString(R.styleable.InfoTextView_rightText); paint = getPaint(); //获取右边的箭头 Drawable drawable = ContextCompat.getDrawable(context, R.drawable.lib_more); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); setCompoundDrawables(null, null, drawable, null); intrinsicWidth = drawable.getIntrinsicWidth(); Log.i(TAG, "InfoTextView: " + paint.getTextSize() + "||" + intrinsicWidth + "||" + drawable.getIntrinsicHeight()); //=========== typedArray.recycle(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Paint.FontMetrics fontMetrics = paint.getFontMetrics(); float top = fontMetrics.top; float bottom = fontMetrics.bottom; Rect rect = new Rect(); paint.getTextBounds(rightText, 0, rightText.length(), rect); x = w - rect.width() - intrinsicWidth;//中间也可以除去padding,自己随便定义 float textY = (bottom + top) / 2; y = h / 2 - textY; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText(rightText, x, y, paint); } //暴露动态设置右边text方法 public void setRightText(String rightText) { this.rightText = rightText; }}
//自定义属性<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="InfoTextView"> <!--设置右边的text--> <attr name="rightText" format="string" /> </declare-styleable></resour
//使用如下图:
最终效果运行效果是第一张图,是不是可以节约很多代码,并且提高了效率
阅读全文
0 0
- 自定义控件InfoTextView展示个人信息
- 自定义歌词展示控件
- XMPP个人信息展示
- 自定义控件:按百分比展示ImageView
- iOS自定义控件-动态标签展示
- jQuery滑过头像图片展示个人信息效果
- Jstree树形控件实现树形结构展示+自定义节点图标
- 通过自定义控件展示矩阵的几何变换
- 闲谈自定义控件源码-view如何展示在界面上
- Android自定义控件显示多页图片展示效果并自动翻页适合广告展示
- 个人信息:
- 个人信息
- 个人信息
- 个人信息
- 个人信息
- 个人信息
- 个人信息
- 个人信息
- Python3.x 连接及写入MySQL
- delphi利用ADO读取Excel 显示在cxgrid
- PHP运算符优先级
- 【学习摘记】马士兵HTML & CSS _课时8_CSS各种效果(含动态面板)
- java 正则 分组
- 自定义控件InfoTextView展示个人信息
- 智能天线调试助手之RFFE协议模拟
- 第十一章——编辑UITableView
- lseek 在结尾添加数据
- linux下查看文件第20-30行内容的N种方法及命令介绍
- POJ 3111 最大化平均值
- python里创建多个协程并发执行
- JVM内存问题Error occurred during initialization of VM
- vue2.0组件之间的通信