代码设置drawableLeft_使用代码为textview设置drawableLeft
2014-11-28 16:17:29  By: shinyuu

Android是使用xml进行布局的、在xml中的textView有一个属性叫drawableLeft、TextView的drawableLeft、drawableRight和drawableTop是一个常用、好用的属性、可以在文本的上下左右放置一个图片、而不使用更加复杂布局就能达到

现在我的需求是这样的、我想在代码中改变drawableLeft、有什么方法可以使用代码为textview设置drawableLeft呢、本文就为大家讲解一下


xml中的textView的drawableLeft是这样设置的

<TextView  
	android:id="@ id/demoTextView"  
	android:layout_width="wrap_content"  
	android:layout_height="wrap_content"  
	android:layout_weight="1"  
	android:drawableLeft="@drawable/drawable_left_icon"  
	android:gravity="center_vertical"  
	android:textStyle="bold"  
	android:textSize="24dp"  
	android:maxLines="1"  
	android:ellipsize="end"/>  


使用代码为textview设置drawableLeft、解决方案是通过TextView的

public void setCompoundDrawables(Drawable left, 
	Drawable top, Drawable right, 
	Drawable bottom);


完整实现代码如下

Drawable drawable= getResources()
	.getDrawable(R.drawable.drawable);  
/// 这一步必须要做,否则不会显示.  
drawable.setBounds(0, 0, drawable.getMinimumWidth(), 
	drawable.getMinimumHeight());  
myTextview.setCompoundDrawables(drawable,null,null,null);  


若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...