TextView Drawalbe文字图标居中显示

你是否遇到这种情况,在TextView宽度或高度过大时,drawable在textveiw所在区域的边缘,但是我们想要的是图片跟随文字居中.

比如我们想要以下效果:

 

WechatIMG2.jpeg

我以前也是遇到过几次,都是偷懒用ImageView代替了。

这次项目中用的地方比较多,果断封装个控件。

效果图:
github : https://github.com/niniloveyou/DrawableTextView

drawableTextView.jpeg

 

如何使用:

添加这两个文件
deadline/DrawableTextView.java
res/values/attrs.xml

代码中这样设置即可

Drawable drawable = ContextCompat.getDrawable(getContext(),R.mipmap.xx);
setDrawable(DrawableTextView.LEFT, drawable, width, height);
  • 1
  • 2
  • 3

xml中定义了以下属性

<deadline.DrawableTextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"

            android:drawablePadding="5dp"

            app:bottomDrawable="@mipmap/camera"
            app:bottomDrawableHeight="20dp"
            app:bottomDrawableWidth="20dp"

            app:leftDrawable="@mipmap/camera"
            app:leftDrawableHeight="20dp"
            app:leftDrawableWidth="20dp"

            app:rightDrawable="@mipmap/camera"
            app:rightDrawableHeight="20dp"
            app:rightDrawableWidth="20dp"

            app:topDrawable="@mipmap/camera"
            app:topDrawableHeight="20dp"
            app:topDrawableWidth="20dp"/>
  •  

github : https://github.com/niniloveyou/DrawableTextView
用的到,Star点下,谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值