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.DrawableEditView
            android: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的例子,不过都是大同小异了!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值