TextView setCompoundDrawables()
来源:互联网 发布:虚拟机怎么解析域名 编辑:程序博客网 时间:2024/05/01 12:00
TextView的 setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)、setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)、setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)三个方法可用于给TextView设置图片,其中setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)方法传入的Drawable必须已调用Drawable.setBounds(Rect bounds)方法给图片设置边界,其他两个方法保持传入图片本质的边界。
/** * Sets the Drawables (if any) to appear to the left of, above, * to the right of, and below the text. Use null if you do not * want a Drawable there. The Drawables must already have had * {@link Drawable#setBounds} called. * * @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(Drawable left, Drawable top, Drawable right, Drawable bottom) { Drawables dr = mDrawables; final boolean drawables = left != null || top != null || right != null || bottom != null; if (!drawables) { // Clearing drawables... can we free the data structure? if (dr != null) { if (dr.mDrawablePadding == 0) { mDrawables = null; } else { // We need to retain the last set padding, so just clear // out all of the fields in the existing structure. if (dr.mDrawableLeft != null) dr.mDrawableLeft.setCallback(null); dr.mDrawableLeft = null; if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null); dr.mDrawableTop = null; if (dr.mDrawableRight != null) dr.mDrawableRight.setCallback(null); dr.mDrawableRight = null; if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null); dr.mDrawableBottom = null; dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; } } } else { if (dr == null) { mDrawables = dr = new Drawables(getContext()); } mDrawables.mOverride = false; if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) { dr.mDrawableLeft.setCallback(null); } dr.mDrawableLeft = left; if (dr.mDrawableTop != top && dr.mDrawableTop != null) { dr.mDrawableTop.setCallback(null); } dr.mDrawableTop = top; if (dr.mDrawableRight != right && dr.mDrawableRight != null) { dr.mDrawableRight.setCallback(null); } dr.mDrawableRight = right; if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { dr.mDrawableBottom.setCallback(null); } dr.mDrawableBottom = bottom; final Rect compoundRect = dr.mCompoundRect; int[] state; state = getDrawableState(); if (left != null) { left.setState(state); left.copyBounds(compoundRect); left.setCallback(this); dr.mDrawableSizeLeft = compoundRect.width(); dr.mDrawableHeightLeft = compoundRect.height(); } else { dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; } if (right != null) { right.setState(state); right.copyBounds(compoundRect); right.setCallback(this); dr.mDrawableSizeRight = compoundRect.width(); dr.mDrawableHeightRight = compoundRect.height(); } else { dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; } if (top != null) { top.setState(state); top.copyBounds(compoundRect); top.setCallback(this); dr.mDrawableSizeTop = compoundRect.height(); dr.mDrawableWidthTop = compoundRect.width(); } else { dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; } if (bottom != null) { bottom.setState(state); bottom.copyBounds(compoundRect); bottom.setCallback(this); dr.mDrawableSizeBottom = compoundRect.height(); dr.mDrawableWidthBottom = compoundRect.width(); } else { dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; } } // Save initial left/right drawables if (dr != null) { dr.mDrawableLeftInitial = left; dr.mDrawableRightInitial = right; } resetResolvedDrawables(); resolveDrawables(); invalidate(); requestLayout(); } /** * 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. * * @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(int left, int top, int right, int bottom) { final Resources resources = getContext().getResources(); setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null, top != 0 ? resources.getDrawable(top) : null, right != 0 ? resources.getDrawable(right) : null, bottom != 0 ? resources.getDrawable(bottom) : null); } /** * Sets the Drawables (if any) to appear to the left of, above, * to the right of, and below the text. Use null if you do not * want a Drawable there. The Drawables' bounds will be set to * their intrinsic bounds. * * @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 setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { if (left != null) { left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight()); } if (right != null) { right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight()); } if (top != null) { top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight()); } if (bottom != null) { bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight()); } setCompoundDrawables(left, top, right, bottom); }
setCompoundDrawablesRelative(Drawable start, Drawable top,Drawable end, Drawable bottom)、setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end,int bottom)、setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top,Drawable end, Drawable bottom)类似。
0 0
- TextView setCompoundDrawables()
- TextView——setCompoundDrawables
- textView-setCompoundDrawables方法解析
- 关于 TextView.setCompoundDrawables() 无效
- TextView setCompoundDrawables不显示
- textview.setCompoundDrawables不显示图片
- TextView——setCompoundDrawables用法
- TextView——setCompoundDrawables用法
- ImageView加TextView可以用setCompoundDrawables代替
- TextView设置setCompoundDrawables或setCompoundDrawablesWithIntrinsicBounds区别
- TextView的setCompoundDrawables不显示问题解决
- TextView,Button 等设置 setCompoundDrawables 无效
- TextView,Button 等设置 setCompoundDrawables 无效
- Android TextView setcompounddrawables 图片不显示的问题
- 获取listview中的子元素view, 设置textview的图标:setCompoundDrawables使用, view.textView.getHeight()为0
- setCompoundDrawables使用
- setCompoundDrawables使用
- android TextView如何通过setCompoundDrawables调整文字和图片的位置(文字显示在图片内)?
- Mybatis分页插件3.2.3版本发布
- 面试手写一个简单的STL容器vector
- Eclipse Java注释模板设置详解
- C++Builder Tchart与TDBchart
- iOS Developer:真机测试
- TextView setCompoundDrawables()
- 二分查找中求中值时要注意溢出
- 善于选择博客源
- java解析TLV格式数据
- 运行springside中的quickstart例子
- 关于iPhone 6的A8芯片我们已经知道这些
- 检测机器接入或移除usb设备
- PAT (Advanced) 1034. Head of a Gang (30)
- Java中float和double类型的数据在内存中是怎样存放的