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()获得。
本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!
阅读全文
1 0
- android EditText的setCompoundDrawables用法
- EditText的setCompoundDrawables用法
- android 的EditText的用法
- android edittext textwatcher的用法
- android EditText的特殊用法
- Android-04 setCompoundDrawables与setCompoundDrawablesWithIntrinsicBounds的区别
- Android setCompoundDrawables无效
- Android EditText的一些小用法
- Android 关于EditText软键盘的用法
- 【Android控件】之EditText的基本用法
- Android TextView setcompounddrawables 图片不显示的问题
- TextView——setCompoundDrawables用法
- TextView——setCompoundDrawables用法
- Android基础-EditText用法
- Android学习:EditText用法
- Android EditText用法大全
- android setCompoundDrawables 不显示问题
- android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
- ORACLE数据库nomount,mount以及open状态
- AES加密
- pdo参数大全
- js中JSON.parse()、JSON.stringify()以及$.param()用法小结
- Linux 文件特殊权限
- android EditText的setCompoundDrawables用法
- python+numpy安装
- git bash 操作命令
- Python数据预处理常用函数
- string--深浅拷贝
- 发现一个VISUAL STUDIO的PROTOBUF的插件,还不错,可以高亮、折叠
- C Primer Plus(第6版)第八章答案
- https://tieba.baidu.com/p/4008858829
- Android ExpandableRecyclerView使用小结