你是否遇到这种情况,在TextView宽度或高度过大时,drawable在textveiw所在区域的边缘,但是我们想要的是图片跟随文字居中.
比如我们想要以下效果:
![](https://img.haomeiwen.com/i695160/3ff26f5b9fbf0905.jpeg)
我以前也是遇到过几次,都是偷懒用ImageView代替了。
这次项目中用的地方比较多,果断封装个控件。
效果图:
github : https://github.com/niniloveyou/DrawableTextView
![](https://img.haomeiwen.com/i695160/9d07da3b4440d256.jpeg)
如何使用:
添加这两个文件
deadline/DrawableTextView.java
res/values/attrs.xml
代码中这样设置即可
Drawable drawable = ContextCompat.getDrawable(getContext(),R.mipmap.xx);
setDrawable(DrawableTextView.LEFT, drawable, width, height);
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点下,谢谢!
可能会有的问题:
由于是对画布的位移实现的,导致foreground可能会显示不正确,
目前还没有想到怎么处理,如果有任何建议,请直接在github上提issue.
网友评论