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()获得。

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值