android EditText的setCompoundDrawables用法

代碼設置圖片相對位置時,常用到如下方法:

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.RemotableViewMethod
public 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()獲得。

本人菜鳥一個,有什麼不對的地方希望大家指出評論,大神勿噴,希望大家一起學習進步!

發佈了132 篇原創文章 · 獲贊 174 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章