本文主要是介绍android 改造TextView使上下左右Drawble宽高可调,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、首先在attrs.xml中声明自定义属性
<declare-styleable name="DrawableEditView"><!-- 设置top图片的宽度 --><attr name="edit_drawableTopWidth" format="dimension" /><!-- 设置top图片的高度 --><attr name="edit_drawableTopHeight" format="dimension" /><attr name="edit_drawableBottomWidth" format="dimension" /><attr name="edit_drawableBottomHeight" format="dimension" /><attr name="edit_drawableRightWidth" format="dimension" /><attr name="edit_drawableRightHeight" format="dimension" /><attr name="edit_drawableLeftWidth" format="dimension" /><attr name="edit_drawableLeftHeight" format="dimension" /><!-- 设置top的图片资源 --><attr name="edit_drawableTop" format="reference" /><attr name="edit_drawableBottom" format="reference" /><attr name="edit_drawableRight" format="reference" /><attr name="edit_drawableLeft" format="reference" />
</declare-styleable>
2、改造TextView
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;
import com.lvzhihao.test.demo.R;
/**
* Created by vzhihao on 2016/4/12.
*/
public class DrawableEditView extends EditText {
private Drawable drawableBottom;
private Drawable drawableTop;
private Drawable drawableLeft;
private Drawable drawableRight;
private int mLeftHeight;
private int mLeftWidth;
private int mRightHeight;
private int mRightWidth;
private int mBottomHeight;
private int mBottomWidth;
private int mTopHeight;
private int mTopWidth;public DrawableEditView(Context context) {super(context);
}public DrawableEditView(Context context, AttributeSet attrs) {super(context, attrs);init(attrs,context);
}private void init(AttributeSet attrs,Context context) {if (attrs != null) {float scale = context.getResources().getDisplayMetrics().density;TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableEditView);int n = a.getIndexCount();for (int i = 0; i < n; i++) {int attr = a.getIndex(i);switch (attr) {case R.styleable.DrawableEditView_edit_drawableBottom:drawableBottom = a.getDrawable(attr);break;case R.styleable.DrawableEditView_edit_drawableTop:drawableTop = a.getDrawable(attr);break;case R.styleable.DrawableEditView_edit_drawableLeft:drawableLeft = a.getDrawable(attr);break;case R.styleable.DrawableEditView_edit_drawableRight:drawableRight = a.getDrawable(attr);break;case R.styleable.DrawableEditView_edit_drawableTopWidth:mTopWidth = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableTopHeight:mTopHeight = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableBottomWidth:mBottomWidth = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableBottomHeight:mBottomHeight = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableRightWidth:mRightWidth = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableRightHeight:mRightHeight = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableLeftWidth:mLeftWidth = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;case R.styleable.DrawableEditView_edit_drawableLeftHeight:mLeftHeight = (int) (a.getDimension(attr, 20) * scale + 0.5f);break;default:break;}}a.recycle();setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);}
}public DrawableEditView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(attrs,context);
}@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {if (left != null) {left.setBounds(0, 0, mLeftWidth <= 0 ? left.getIntrinsicWidth() : mLeftWidth, mLeftHeight <= 0 ? left.getMinimumHeight() : mLeftHeight);}if (right != null) {right.setBounds(0, 0, mRightWidth <= 0 ? right.getIntrinsicWidth() : mRightWidth, mRightHeight <= 0 ? right.getMinimumHeight() : mRightHeight);}if (top != null) {top.setBounds(0, 0, mTopWidth <= 0 ? top.getIntrinsicWidth() : mTopWidth, mTopHeight <= 0 ? top.getMinimumHeight() : mTopHeight);}if (bottom != null) {bottom.setBounds(0, 0, mBottomWidth <= 0 ? bottom.getIntrinsicWidth() : mBottomWidth, mBottomHeight <= 0 ? bottom.getMinimumHeight(): mBottomHeight);}setCompoundDrawables(left, top, right, bottom);
}
}
3、在布局文件中使用
<com.lvzhihao.test.demo.view.DrawableEditViewandroid:layout_weight="5"android:layout_width="0dp"android:layout_height="wrap_content"app:edit_drawableRight="@mipmap/ic_launcher"app:edit_drawableRightHeight="8dp"app:edit_drawableRightWidth="8dp"android:hint="先生"/>
4、注意在根布局中要引入
xmlns:app=”http://schemas.android.com/apk/res-auto”
5、现在你是不是也会写
DrableButtonView、DrableTextView、DrableEditView
不好意思,标题是textview,却用了editview的例子,不过都是大同小异了!
这篇关于android 改造TextView使上下左右Drawble宽高可调的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!