Android 自己定义 TextView drawableTop 图标与文字左对齐(效果图)
Android 自己 定义 文字 图标 对齐 textview 效果图
2023-09-14 09:07:58 时间
public class DrawableTopLeftTextView extends TextView { private Paint mPaint; private float fFontHeight; private Drawable[] drawables; private int leftMargin = 40; //TODO 这个要通过代码获取,不能硬编码 public DrawableTopLeftTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public DrawableTopLeftTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DrawableTopLeftTextView(Context context) { super(context); init(); } private void init() { mPaint = getPaint(); //mPaint.setTextSize(56.0f); mPaint.setColor(Color.parseColor("#cccccccc")); FontMetrics fm = mPaint.getFontMetrics(); fFontHeight = (float) Math.ceil(fm.descent - fm.ascent); drawables = getCompoundDrawables(); } @Override protected void onDraw(Canvas canvas) { if (drawables != null) { Drawable drawable = drawables[1]; // top if (drawable != null) { final float textY = getY() + drawable.getIntrinsicHeight() + fFontHeight - 5; canvas.drawText(getText().toString(), 0, textY, mPaint); canvas.clipRect(drawable.getBounds()); canvas.drawBitmap(getBitmap(drawable), getLeft()+leftMargin, getTop(), mPaint); drawable.draw(canvas); canvas.save(); } } super.onDraw(canvas); } private final static Bitmap getBitmap(Drawable drawable){ BitmapDrawable bd = (BitmapDrawable) drawable; return bd.getBitmap(); } }
<com.bluetooth.light.widget.DrawableTopLeftTextView android:id="@+id/signal_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:drawableTop="@drawable/signal_logo" android:text="@string/signal" android:textColor="#cccccccc" android:textSize="24sp" />效果图
相关文章
- android移动点餐系统内容和要求,基于Android云计算的移动点餐系统
- android进阶之了解Android系统与开机过程
- android 安装p12证书,如何在Android中使用p12证书(客户端证书)
- android vlc 中文字幕,解决Android版vlc中文乱码问题
- Android SDK安装及配置模拟器[通俗易懂]
- android 定时器实例,Android定时器和Handler用法实例分析
- iphone4装android,iPhone4可安装Android实现双系统启动.pdf
- android toast全屏,Android Toast实现全屏显示
- Android NDK 常见面试题
- 【Android RTMP】音频数据采集编码 ( FAAC 编码器编码 AAC 音频解码信息 | 封装 RTMP 音频数据头 | 设置 AAC 音频数据类型 | 封装 RTMP 数据包 )
- WeTest领先推出Android 14系统云手机,欢迎体验!
- 【Android Gradle 插件】BuildType 编译类型配置 ① ( BuildType 编译类型简介 | BuildType#applicationIdSuffix 包名后缀配置 )
- 【错误记录】Android Studio 编译时 lint 检查报错 ( WARNING: DSL element ‘android.dataBinding.enabled‘ is obsolet )
- [android] notification入门详解手机开发
- Android Studio集成Genymotion详解手机开发
- android图片处理(圆角,压缩)详解编程语言
- Android系统与Linux之间的联系(android和linux)
- asp.net通过UserAgent判断智能设备(Android,IOS)
- 配置一个好用的Android模拟器让你不再对模拟器那么失望
- android播放视频时在立体声与单声道之间切换无变化原因分析及解决
- Android修改自己程序字体的方法详解
- Android获取经纬度计算距离介绍
- android教程之把自己的应用加入到系统分享中
- Android实现检查并下载APK更新、安装APK及获取网络信息的方法
- Android开发之登录验证实例教程