android EditText的setCompoundDrawables用法

来源:互联网 发布:即时通讯软件源码 编辑:程序博客网 时间:2024/05/21 22:54

代码设置图片相对位置时,常用到如下方法:

setCompoundDrawables(left, top, right, bottom)

意思:设置Drawable显示在text的左、上、右、下位置。

方法的源码:

/** * Sets the Drawables (if any) to appear to the left of, above, to the * right of, and below the text. Use {@code null} if you do not want a * Drawable there. The Drawables must already have had * {@link Drawable#setBounds} called. * <p> * Calling this method will overwrite any Drawables previously set using * {@link #setCompoundDrawablesRelative} or related methods. * * @attr ref android.R.styleable#TextView_drawableLeft * @attr ref android.R.styleable#TextView_drawableTop * @attr ref android.R.styleable#TextView_drawableRight * @attr ref android.R.styleable#TextView_drawableBottom */public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top,        @Nullable Drawable right, @Nullable Drawable bottom) {    Drawables dr = mDrawables;    // We're switching to absolute, discard relative.    if (dr != null) {        if (dr.mDrawableStart != null) dr.mDrawableStart.setCallback(null);        dr.mDrawableStart = null;        if (dr.mDrawableEnd != null) dr.mDrawableEnd.setCallback(null);        dr.mDrawableEnd = null;        dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0;        dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0;    }

当然还有一种设置方式(意思是一样的):

setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)

方法源码:

/** * Sets the Drawables (if any) to appear to the left of, above, to the * right of, and below the text. Use 0 if you do not want a Drawable there. * The Drawables' bounds will be set to their intrinsic bounds. * <p> * Calling this method will overwrite any Drawables previously set using * {@link #setCompoundDrawablesRelative} or related methods. * * @param left Resource identifier of the left Drawable. * @param top Resource identifier of the top Drawable. * @param right Resource identifier of the right Drawable. * @param bottom Resource identifier of the bottom Drawable. * * @attr ref android.R.styleable#TextView_drawableLeft * @attr ref android.R.styleable#TextView_drawableTop * @attr ref android.R.styleable#TextView_drawableRight * @attr ref android.R.styleable#TextView_drawableBottom */@android.view.RemotableViewMethodpublic void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left,        @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {    final Context context = getContext();    setCompoundDrawablesWithIntrinsicBounds(left != 0 ? context.getDrawable(left) : null,            top != 0 ? context.getDrawable(top) : null,            right != 0 ? context.getDrawable(right) : null,            bottom != 0 ? context.getDrawable(bottom) : null);}

但是二者是有些区别:
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,
所以才有The Drawables must already have had setBounds(Rect) called.
使用之前必须使用Drawable.setBounds设置Drawable的长宽。

setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,
所以才有The Drawables’ bounds will be set to their intrinsic bounds.
即通过getIntrinsicWidth()与getIntrinsicHeight()获得。

本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!