textview 使drawable与text一起居中的textview,这里仅支持drawableleft
package cc.hent.www.ramo_cmedcial.CustomView; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet; /**
* Created by he cs on 2018/1/2.
* drawable与text一起居中的textview
*/ public class DrawableTextView extends android.support.v7.widget.AppCompatTextView {
public DrawableTextView (Context context) {
this(context,null);
} public DrawableTextView (Context context, @Nullable AttributeSet attrs) {
this(context, attrs,-1);
} public DrawableTextView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onDraw (Canvas canvas) { Drawable[] compoundDrawables = getCompoundDrawables();
if (compoundDrawables != null) {
Drawable left = compoundDrawables[0];
if (left != null) {
float textWidth = getPaint().measureText(getText().toString());
int compoundDrawablePadding = getCompoundDrawablePadding();
//获取drawable的宽度 Intrinsic 内在的
int leftWidth = left.getIntrinsicWidth(); float bodyWidth = textWidth+compoundDrawablePadding+leftWidth; canvas.translate((getWidth()-bodyWidth)/2,0);
}
} //一定要,后调用
super.onDraw(canvas);
} }
重点在于重写ondraw,其他属性同textview就可以,注意,在使用gravity的时候,center_vertical即可
textview 使drawable与text一起居中的textview,这里仅支持drawableleft的更多相关文章
- 它们的定义TextView使之具有跑马灯的效果
一.引入问题 使用通用textview快乐效应,焦点事件不启动滚动,button目前的焦点事件,但丑,因此,需要定制TextView 天生焦点 个textview FocusedTextView.ja ...
- JavaScript通过preventDefault()使input[type=text]禁止输入但保留光标
一.说明 取消事件的默认动作. 该方法将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作).例如,如果 type 属性是 "submit",在事件传播的任意阶段 ...
- android 自定义空间 组合控件中 TextView 不支持drawableLeft属性
android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...
- textview设置drawable
textview可以在上下左右四个方向添加图片,同时也可以动态改变这些图片: 下面有我写的一个例子: 在xml文件中: <TextView android: ...
- 对TextView设置drawable,用setCompoundDrawables方法实现
在上一项目上需要对TextView在xml文件中设置的drawableLeft的图片进行更改,查询了资料好久也没有找到解决办法,如下代码所示: commentTV.setCompoundDrawabl ...
- jQery使网页在显示器上居中显示适用于任何分辨率
这篇文章主要介绍了jQery使网页在任何分辨率的显示器上居中显示的方法,需要的朋友可以参考下 检测屏幕宽度,并设置为id为frame的div宽度, 根据自己网页的最大宽度来调节,小demo最大宽度为1 ...
- Android TextView : “Do not concatenate text displayed with setText”
参考:http://stackoverflow.com/questions/33164886/android-textview-do-not-concatenate-text-displayed-wi ...
- 使用java代码,动态给TextView设置drawable
Drawable country = context.getResources().getDrawable(drawableId); country.setBounds(0, 0, country.g ...
- HTML5 Canvas Text文本居中实例
1.代码: <canvas width="700" height="300" id="canvasOne" class="c ...
随机推荐
- sprint冲刺(第二天)
今天的每日立会是在早上早餐后8点在宿舍讨论的,大概讨论了关于四则运算练习器APP的看法,也对一些较为基础的功能进行说明
- 团队作业4Alpha冲刺(真.三英战吕布团队)
第一天 2018/6/13 1.1 今日完成任务情况以及遇到的问题. 1.1.1:完成前台部分界面优化,后台进行代码优化 1.1.2团队前台部分js.jquery部分功能实现有难度. 1.2 明天任务 ...
- java下Mysql基本操作
https://www.cnblogs.com/centor/p/6142775.html
- static作用(修饰函数、局部变量、全局变量)
转自:http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作 ...
- react +MUI checkbox使用
PS:因项目采用MUI,故,在此所讲的checkbox组件为MUI里的checkbox 因checkbox组件里 oncheck函数没法判断复选框是否选中,故,若直接复用且通过state改变check ...
- 二叉查找树ADT--C语言描述
首先给出此ADT的声明: struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree ...
- Python实现奖金计算两种方法的比较
应发奖金计算 简述:企业发放的奖金根据利润提成.利润(profit)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成 ...
- BZOJ3512 DZY Loves Math IV(杜教筛+线性筛)
注意到n很小,考虑枚举i.现在要求的是f(n,m)=Σφ(in) (i=1~m).显然当n没有平方因子时,φ(in)=φ(i)·φ(n/gcd(i,n))·gcd(i,n).利用φ*1=id又可得φ( ...
- P3293 [SCOI2016]美味
题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...
- BeautifulSoup的find()和findAll()
BeautifulSoup的提供了两个超级好用的方法(可能是你用bs方法中最常用的).借助这两个函数,你可以通过表现的不同属性轻松过滤HTML(XML)文件,查找需要的标签组或单个标签. 首先find ...