如何在代码中动态为TextView代码设置drawableeRight

Android 动态设置TextView的drawableLeft等属性
首先,我们在开发过程中,会经常使用到android:drawableLeft=&@drawable/ic_launcher&这些类似的属性:
关于这些属性的意思,无非是在你的textView文本的上下左右处添加一个图片。
首先,我们在开发过程中,会经常使用到android:drawableLeft="@drawable/ic_launcher"这些类似的属性:
关于这些属性的意思,无非是在你的textView文本的上下左右处添加一个图片。比如下面这么一段代码:
android:id="@+id/text_drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="4dp"
它设置了在文本的左边,显示一个小图标,效果如下:
而在一些情况下,我们需要在动态在代码中设置文本周围的图标,那该如何呢,首先,我们看下TextView提供了哪些方法:
乍眼看去,挺多方法的,好,我们主要介绍setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds。
手工设置文本与图片相对位置时,常用到如下方法:
setCompoundDrawables(left, top, right, bottom)及setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom),它们的意思是设置Drawable显示在text的左、上、右、下位置。
但是两者有些区别:
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()获得。
一般,建议使用setCompoundDrawablesWithIntrinsicBounds,这样你即无需设置Drawables的bounds了。
看下代码:
TextView textDrawable = (TextView) findViewById(R.id.text_drawable);
Drawable drawableLeft = getResources().getDrawable(
R.drawable.ic_launcher);
textDrawable.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
null, null, null);
textDrawable.setCompoundDrawablePadding(4);
效果和以上直接通过android:drawableLeft一样!
版权声明:本文内容由互联网用户自发贡献,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至: 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
用云栖社区APP,舒服~
【云栖快讯】红轴机械键盘、无线鼠标等753个大奖,先到先得,云栖社区首届博主招募大赛9月21日-11月20日限时开启,为你再添一个高端技术交流场所&&
一项针对阿里云资源和互联网应用进行监控的服务。云监控服务可用于收集获取阿里云资源的监控指标,探测互联网服务可用性...
是基于语音识别、语音合成、自然语言理解等技术,为企业在多种实际应用场景下,赋予产品“能听、会说、懂你”式的智能人...
基于阿里聚安全的核心技术,为移动应用(APP)提供全生命周期的安全服务,其能够准确发现应用的安全漏洞,恶意代码,...
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本...
阿里云双11狂欢,不只是5折
Loading...1522人阅读
安卓整理(170)
& & 用户对Android应用的体验要求是越来越高,对apk的界面要求也是与日俱增。不仅要实现功能 ,还要实现图文并茂的效果。有时候我们要在某一段文字的或上或下或左或右添加一张图片,来表达某种效果,比如在倒计时的时间前面放个时钟图片,显示更生动吧。今天我们就来实现这样的简单效果。
& &方法1:就是在显示文字对应的TextView要显示图片方位上放一个ImageVeiw,这样做是不是很简单呢?其它更简单的是方法2;
& &方法2:就是用到TextView自带属性drawableLeft/drawableRight/drawableTop/drawableBottom属性,这样更简单。
----------------------------------xml举例-------------------------------------------------------------
& & & & & & & & & & android:id=&@+id/count_down_time_tv&
& & & & & & & & & & android:layout_width=&wrap_content&
& & & & & & & & & & android:layout_height=&wrap_content&
& & & & & & & & & & android:text=&要开始计时了&
& & & & & & & & & & android:drawableLeft=&@drawable/my_test_time&&!--这指把图片放到文字左边--&
& & & & & & & & & & android:drawablePadding=&5dp&&!--文字与图片的间距--&
& & & & & & & & & & android:textColor=&#ea78ab&
& & & & & & & & & & android:textSize=&13sp& /&
这样把TextView展示出来效果就是左边图片加文字效果。其它几个方向是一样的。不过有时候我们要在代码中灵活控件是否要显示图片呢?比如时间倒计时结束,图片就消息,文字也跟着改变怎么办?那就在代码中来动态改变图片吧
--------------------------------在代码中动态改变drawable----------------------------------------------------------------------
& & & & & & & & & &private TextView&nick_tv
& & & & & & & & & &Drawable drawable = getResources().getDrawable(R.drawable.my_nike_name_arow);
/**这一步必须要做,否则不会显示.*/
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//对图片进行压缩
& & & & & & & & & & &/**设置图片位置,四个参数分别方位是左上右下,都设置为null就表示不显示图片*/
nick_tv.setCompoundDrawables(null, null, drawable, null);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:716144次
积分:10496
积分:10496
排名:第1709名
原创:341篇
转载:58篇
评论:233条
个人Github主页面:/ldm520,请大家多指教!
(1)(2)(2)(2)(2)(3)(7)(7)(2)(2)(4)(8)(4)(17)(13)(16)(18)(15)(26)(13)(12)(23)(21)(5)(6)(11)(19)(9)(10)(13)(37)(31)(12)(16)(1)(10)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。4569人阅读
Android自定义控件(18)
代码动态布局,并需要为每一个条目设置图标,此时用到了&android:drawableLeft=&@drawable/icon&&
父xml文件:
&?xml version=&1.0& encoding=&utf-8&?&
&ScrollView xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:background=&@color/background& &
&!-- 子布局由代码动态生成 --&
&LinearLayout
android:id=&@+id/layout_CONTENT&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:padding=&@dimen/content_padding& &
&LinearLayout
android:id=&@+id/activity_service_select&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_margin=&@dimen/table_margin&
android:background=&@color/table_background&
android:orientation=&vertical&
android:padding=&@dimen/table_padding& &
&/LinearLayout&
&/LinearLayout&
&/ScrollView&
子xml文件:
&RelativeLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&@dimen/row_height&
android:layout_marginBottom=&@dimen/row_margin&
android:background=&@drawable/row_selector&
android:paddingLeft=&@dimen/row_padding_left&
android:paddingRight=&@dimen/row_padding_right& &
android:id=&@+id/tv_select_item&
style=&@style/text_18&
android:layout_width=&match_parent&
android:layout_height=&@dimen/row_height&
android:layout_marginBottom=&@dimen/row_margin&
android:background=&@drawable/row_selector&
android:gravity=&center_vertical&
android:textColor=&@drawable/row_text_selector& /&
&ImageView
android:id=&@+id/iv_icon&
android:layout_width=&wrap_content&
android:layout_height=&match_parent&
android:layout_alignParentRight=&true&
android:duplicateParentState=&true&
android:gravity=&center_vertical&
android:src=&@drawable/go& /&
&/RelativeLayout&
代码中引用:
private ViewGroup mL
private int img[] = {R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};
/* (non-Javadoc)
* @see app.ui.TitleActivity#onCreate(android.os.Bundle)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpViews();
private void setUpViews()
setContentView(R.layout.activity_service_select);
setTitle(R.string.text_select);
showBackwardView(R.string.button_backward, true);
mLayout = (ViewGroup)findViewById(R.id.activity_service_select);
final String [] mSelfSelect = getResources().getStringArray(R.array.text_self_select);
// 需要布局的行数
final int rowCount = mSelfSelect.
for (int i = 0; i & rowC i++) {
final LinearLayout linearLayout = new LinearLayout(this);
View.inflate(this, R.layout.service_examvaluable_item, linearLayout);
final View view = linearLayout.getChildAt(0);
view.setTag(i+1);
view.setOnClickListener(this);
Drawable drawable= getResources().getDrawable(img[i]);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
final TextView mTextView = (TextView) linearLayout.findViewById(R.id.tv_select_item);
mTextView.setCompoundDrawables(drawable,null,null,null);//设置TextView的drawableleft
mTextView.setCompoundDrawablePadding(10);//设置图片和text之间的间距
mTextView.setText(mSelfSelect[i]);
// 添加到屏幕布局
LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
mLayout.addView(linearLayout, layoutParams);
在程序中直接取出子xml中TextView中的id,并动态设置改变了 DrawableLeft。
解决方案:
类似调用方法如下:
1.在XML中使用
2.代码中动态变化
参考另一个函数:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1512695次
积分:15837
积分:15837
排名:第734名
原创:216篇
评论:592条
欢迎加入讨论群,相互学习,共同进步
文章:23篇
阅读:125829
阅读:61929
阅读:48572
文章:40篇
阅读:278541
文章:17篇
阅读:101411
文章:30篇
阅读:198660
(4)(2)(1)(1)(1)(3)(1)(4)(3)(14)(13)(9)(9)(18)(19)(6)(9)(15)(14)(7)(23)(3)(26)(12)(1)(3)(KNKSLEE_)
第三方登录:}

我要回帖

更多关于 代码设置drawable 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信