在TextView中以编程方式设置左可绘制 [英] Programmatically set left drawable in a TextView

查看:101
本文介绍了在TextView中以编程方式设置左可绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xml中有一个textView.

I have a textView in xml here.

<TextView
        android:id="@+id/bookTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableLeft="@drawable/checkmark"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="24dip"
        android:maxLines="1"
        android:ellipsize="end"/>

如您所见,我在xml中设置了DrawableLeft.

As you can see I set the DrawableLeft in xml.

我想更改代码中的drawable.

I would like to change the drawable in code.

反正有这样做吗?还是在代码中为文本视图设置drawableLeft?

Is there anyway to go about doing this? Or setting the drawableLeft in code for the text view?

推荐答案

您可以使用在不需要图像的位置设置0

set 0 where you don't want images

左侧可绘制示例:

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);


提示:只要您知道任何XML属性,但对如何在运行时使用它一无所知.只需在开发人员文档中转到该属性的描述即可.如果运行时支持相关方法,则可以在其中找到.即对于DrawableLeft


Tip: Whenever you know any XML attribute but don't have clue about how to use it at runtime. just go to the description of that property in developer doc. There you will find Related Methods if it's supported at runtime . i.e. For DrawableLeft

这篇关于在TextView中以编程方式设置左可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆