今天分享一个Layout布局中的一个小技巧,希望看过之后你也可以写出性能更好的布局,我个人的目的是用最少的view写出一样的效果布局
用TextView同时显示图片和文字:
先看一下效果图
以上这四块区域相信大家在项目中经常遇到吧!(一般的写法ImageView与TextView的组合)现在用一个自定义的TextView就完成能达到一样的效果,并且也可以设置背景选择器、图片的尺寸大小,不需要嵌套多层布局在设置相关的属性
第一块Xml中的代码
="" <="" code="">
下面的分割线我建议用Space这个控件,它是一个非常轻量级的控件
第二块Xml中的代码
<="" code="">
第三块Xml中的代码
<="" code="">
第四块Xml中的代码(图片按钮)
<="" code="">
下面贴出TextDrawable.java代码
/**
* Created by Dengxiao on 2016/11/8.
*/
publicclassTextDrawable extends TextView {
privateDrawable drawableLeft;
privateDrawable drawableRight;
privateDrawable drawableTop;
privateintleftWidth;
privateintrightWidth;
privateinttopWidth;
privateintleftHeight;
privateintrightHeight;
privateinttopHeight;
publicTextDrawablef(Context context){
this(context, null, 0);
}
publicTextDrawable(Context context, AttributeSet attrs){
this(context, attrs, 0);
}
publicTextDrawable(Context context, AttributeSet attrs, intdefStyleAttr){
super(context, attrs, defStyleAttr);
init(context, attrs);
}
privatevoidinit(Context context, AttributeSet attrs){
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextDrawable);
drawableLeft = typedArray.getDrawable(R.styleable.TextDrawable_leftDrawable);
drawableRight = typedArray.getDrawable(R.styleable.TextDrawable_rightDrawable);
drawableTop = typedArray.getDrawable(R.styleable.TextDrawable_topDrawable);
if(drawableLeft != null) {
leftWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_leftDrawableWidth, DensityUtils.dip2px(context, 20));
leftHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_leftDrawableHeight, DensityUtils.dip2px(context, 20));
}
if(drawableRight != null) {
rightWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_rightDrawableWidth, DensityUtils.dip2px(context, 20));
rightHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_rightDrawableHeight, DensityUtils.dip2px(context, 20));
}
if(drawableTop != null) {
topWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_topDrawableWidth, DensityUtils.dip2px(context, 20));
topHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_topDrawableHeight, DensityUtils.dip2px(context, 20));
}
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec, intheightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(drawableLeft != null) {
drawableLeft.setBounds(0, 0, leftWidth, leftHeight);
}
if(drawableRight != null) {
drawableRight.setBounds(0, 0, rightWidth, rightHeight);
}
if(drawableTop != null) {
drawableTop.setBounds(0, 0, topWidth, topHeight);
}
}
@Override
protectedvoidonDraw(Canvas canvas){
super.onDraw(canvas);
this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, null);
}
/**
* 设置左侧图片并重绘
*/
publicvoidsetDrawableLeft(Drawable drawableLeft){
this.drawableLeft = drawableLeft;
invalidate();
}
/**
* 设置左侧图片并重绘
*/
publicvoidsetDrawableLeft(intdrawableLeftRes){
this.drawableLeft = UIUtils.getContext().getResources().getDrawable(drawableLeftRes);
invalidate();
}
/**
* 设置右侧图片并重绘
*/
publicvoidsetDrawableRight(Drawable drawableRight){
this.drawableRight = drawableLeft;
invalidate();
}
/**
* 设置右侧图片并重绘
*/
publicvoidsetDrawableRight(intdrawableRightRes){
this.drawableRight = UIUtils.getContext().getResources().getDrawable(drawableRightRes);
invalidate();
}
/**
* 设置上部图片并重绘
*/
publicvoidsetDrawable(Drawable drawableTop){
this.drawableTop = drawableTop;
invalidate();
}
/**
* 设置右侧图片并重绘
*/
publicvoidsetDrawableTop(intdrawableTopRes){
this.drawableTop = UIUtils.getContext().getResources().getDrawable(drawableTopRes);
invalidate();
}
}