Android TextView的drawLeft无法与文字一起居中解决方案
来源:互联网 发布:js在框架在打开 编辑:程序博客网 时间:2024/06/06 03:45
TextView的drawableLeft、drawableRight和drawableTop不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,重写一下TextView即可解决。
- /**
- * drawableLeft与文本一起居中显示
- *
- *
- */
- public class DrawableCenterTextView extends TextView {
- public DrawableCenterTextView(Context context, AttributeSet attrs,
- int defStyle) {
- super(context, attrs, defStyle);
- }
- public DrawableCenterTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public DrawableCenterTextView(Context context) {
- super(context);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Drawable[] drawables = getCompoundDrawables();
- if (drawables != null) {
- Drawable drawableLeft = drawables[0];
- if (drawableLeft != null) {
- float textWidth = getPaint().measureText(getText().toString());
- int drawablePadding = getCompoundDrawablePadding();
- int drawableWidth = 0;
- drawableWidth = drawableLeft.getIntrinsicWidth();
- float bodyWidth = textWidth + drawableWidth + drawablePadding;
- canvas.translate((getWidth() - bodyWidth) / 2, 0);
- }
- }
- super.onDraw(canvas);
- }
- }
0 0
- Android TextView的drawLeft无法与文字一起居中解决方案
- Android 中TextView的DrawableLeft与DrawableRight与文字一起居中显示
- 让TextView的drawable与文字一起居中
- Android TextView的drawLeft、drawRight..
- 【Android 自定义控件】TextView的drawable与text一起居中
- Android TextView 文字居中
- Android TextView 文字居中 .
- Android TextView 文字居中
- Android TextView 文字居中
- Android TextView 文字居中
- Android TextView 文字居中
- Android的TextView中的文字居中显示
- Android中TextView文字居中与垂直靠左居中
- 解决ListView中TextView文字无法正常居中的问题
- supportsRtl属性影响TextView的textAlignment属性,文字无法居中
- 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
- 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
- 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
- 实用的分页插件
- ranong项目总结-HttpClient-Timeout设置(一)
- 马踏棋盘算法(骑士周游问题)
- 使用Android Studio搭建开发环境过程
- vue.js入门
- Android TextView的drawLeft无法与文字一起居中解决方案
- 进阶篇 react + webpack 配合
- 迭代器模式
- BZOJ 1024 dfs
- 字符串截取
- Okgo 使用三部曲
- 通过gdal加载PostGIS矢量数据
- 基于JavaMail的Java邮件发送:简单邮件发送
- win10环境下visual studio 2015配置openGL