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
Android: how to add Icon at the left side of the TextView
提问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 drawable
and textview
by
你也可以把之间的填充drawable
和textview
通过
android:drawablePadding="2dp"
If you always want an icon to appear before the text, it is recommended to use drawableStart
instead of drawableLeft
since 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 TextView
and specify a drawable there your want to present on the left side of it.
对于您TextView
并指定要在其左侧呈现的可绘制对象。