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的例子,不過都是大同小異了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章