Android:如何在TextView的左侧添加Icon

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25279715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:18:02  来源:igfitidea点击:

Android: how to add Icon at the left side of the TextView

androidtextview

提问by user3519555

I want to add icon at the left side of the textView.How can I do that?

我想在 textView 的左侧添加图标。我该怎么做?

回答by krunal patel

You can use:

您可以使用:

android:drawableLeft="@drawable/ic_launcher"

and you can also put padding between drawableand textviewby

你也可以把之间的填充drawabletextview通过

android:drawablePadding="2dp"

If you always want an icon to appear before the text, it is recommended to use drawableStartinstead of drawableLeftsince many languages are not read left to right.

如果您总是希望在文本之前出现一个图标,建议使用drawableStart而不是drawableLeft因为许多语言不是从左到右阅读的。

回答by Robin Royal

You can do this using this code.

您可以使用此代码执行此操作。

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

回答by Emil Adz

You can use this in your XML file:

您可以在 XML 文件中使用它:

android:drawableLeft

For your TextViewand specify a drawable there your want to present on the left side of it.

对于您TextView并指定要在其左侧呈现的可绘制对象。