public class CustomDrawableTextView extends TextView{

    //image width、height
private int imageWidth;
private int imageHeight; private Drawable leftImage;
private Drawable topImage;
private Drawable rightImage;
private Drawable bottomImage; public CustomDrawableTextView(Context context) {
this(context, null);
}
public CustomDrawableTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomDrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomDrawableTextView,0,0);
int countNum = ta.getIndexCount();
for (int i = 0; i < countNum; i++) { int attr = ta.getIndex(i);
if (attr == R.styleable.CustomDrawableTextView_leftImage) {
leftImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_topImage) {
topImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_rightImage) {
rightImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_bottomImage) {
bottomImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_imageWidth) {
imageWidth = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
} else if (attr == R.styleable.CustomDrawableTextView_imageHeight) {
imageHeight = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
}
} ta.recycle();
init();
} /**
* init views
*/
private void init() {
setCompoundDrawablesWithIntrinsicBounds(leftImage,topImage,rightImage,bottomImage);
} @Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { if(left != null) {
left.setBounds(0,0,imageWidth,imageHeight);
} if(top != null) {
top.setBounds(0,0,imageWidth,imageHeight);
} if(right != null) {
right.setBounds(0,0,imageWidth,imageHeight);
} if(bottom != null) {
bottom.setBounds(0,0,imageWidth,imageHeight);
} setCompoundDrawables(left,top,right,bottom);
}
}
<declare-styleable name="CustomDrawableTextView" >
<attr name="leftImage" format="reference" />
<attr name="topImage" format="reference" />
<attr name="rightImage" format="reference" />
<attr name="bottomImage" format="reference" />
<attr name="imageWidth" format="dimension" />
<attr name="imageHeight" format="dimension" />
</declare-styleable>
app:imageHeight="50dp" //图片高度

app:imageWidth="50dp" //图片宽度

app:leftImage="@drawable/ic_qq" //左部图片

app:topImage="@drawable/ic_qq" //顶部图片

app:rightImage="@drawable/ic_qq" //右部图片

app:bottomImage="@drawable/ic_qq" //底部图片

compile 'com.github.czy1121:roundbutton:1.0.0'
 compile 'com.song:CustomDrawableTextView:1.0.0'
 

CustomDrawableTextView的更多相关文章

随机推荐

  1. [jzoj]1729.blockenemy

    Link https://jzoj.net/senior/#main/show/1729 Description 你在玩电子游戏的时候遇到了麻烦...... 你玩的游戏是在一个虚拟的城市里进行,这个城 ...

  2. 资源从3ds max导入UE4问题

    1.先删掉灯光和相机.材质命名为英文,贴图也要英文取名,不能有中文,并且必须是JPG格式.并整理好组:删掉多余的物体,例如线2.坐标归零.并把材质转换为默认材质3.选中一个组,先unground,然后 ...

  3. sweetalert的使用

    1.swal()方法中的参数: 2.引入css与js,通过cdn加速服务 <link href="https://cdnjs.cloudflare.com/ajax/libs/swee ...

  4. 将两个DataTable合并成一个DataTable

    转载自 http://blog.csdn.net/wangxiaojia42121/article/details/53330464 谢谢 //两个结构一样的DT合并DataTable DataTab ...

  5. 【转】Java基础:System.out.println与System.err.println的区别

    同时使用了System.out.println与System.err.println()打印输入内容,结果看到的内容和预想的不一样,顺序与预料的不同并不是因为err和out的区别导致,而是因为他们是两 ...

  6. ISDN简记

    简介 ISDN:(Integrated Services Digital Network,综合业务数字网) 是以综合数字电话网(IDN)为基础发展演变而形成的通信网,能够提供端到端的数字连接,用来支持 ...

  7. C/JS_实现选择排序

    1.js var arr = prompt("请输入一个数组(以“,”隔开):").split(",").map(function(data){ return ...

  8. Mac删除默认美国输入法

    1.打开sudo open ~/Library/Preferences/com.apple.HIToolbox.plist 2.找到这个,然后点击删除,最后保存,然后在立马重启 3.如果要还原,直接在 ...

  9. SpringMVC的拦截器(Interceptor)和过滤器(Filter)的区别与联系

    摘自: http://blog.csdn.net/xiaoyaotan_111/article/details/53817918 一 简介 (1)过滤器: 依赖于servlet容器.在实现上基于函数回 ...

  10. SpringBoot(九):多模块下mapper分散后无法启动SpringBoot解决方法

    问题描述: SpringBoot项目之前是一个项目*-web,运行没问题,后来将项目拆分为多个项目,就出现启动失败问题. SpringBoot项目结构: 项目被分为多块:*-mapper(mybati ...