在 Android 中的 EditText 左侧设置图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12444320/
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
Set Image on Left side of EditText in Android
提问by Scorpion
I am having one contact form in which I would like to add some icons to my edit text for a better look. I want to create EditText something like below.
我有一个联系表格,我想在其中添加一些图标到我的编辑文本中以获得更好的外观。我想创建类似下面的 EditText 。
I have EditText like below. And I got the result something like. I want the image to be absolutely on the left side as it is in the above image. I know that internally EditText is using Nine Patch image and i think that is why the result is some what different. Can anyone know how to resolve this?
我有像下面这样的 EditText。我得到了类似的结果。我希望图像绝对在左侧,如上图所示。我知道 EditText 在内部使用九补丁图像,我认为这就是结果有些不同的原因。谁能知道如何解决这个问题?
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="51dip"
android:hint="@string/name"
android:inputType="text"
android:drawableLeft="@drawable/name_icon_edittext" />
回答by Tomer Mor
Instead of using android:drawableLeft="@drawable/name_icon_edittext"
, create separateImageView
for the name_icon
.
This way you have more control on placing it on the layout
而不是使用android:drawableLeft="@drawable/name_icon_edittext"
,ImageView
为name_icon
. 这样您就可以更好地控制将其放置在layout
Note:I am assuming your layout
is RelativeLayout
注意:我假设你layout
是RelativeLayout
回答by Faris Jameel
if you want to add some space between you text and image then use'drawablePadding' property in xml
如果要在文本和图像之间添加一些空格,请在 xml 中使用“drawablePadding”属性
android:drawablePadding="10dp"
here is complete example how to use in edittext in xml file
这是如何在 xml 文件中的 edittext 中使用的完整示例
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_left"
android:drawablePadding="10dp" />
回答by Milind App
You can use android:drawablePadding="10dp"
property in the xml file.
There's no need to create separate imagview for the icon.
您可以android:drawablePadding="10dp"
在 xml 文件中使用属性。无需为图标创建单独的 imagview。
回答by Armando
A bit old, but for future reference maybe:
有点旧,但为了将来参考也许:
I would do a 9-patch image myself with the appearance you want, keeping the proportions and all but the icon, so you can add it yourself (and reuse the background) with drawableLeft
property of EditText
. That way only the middle part of the image will be stretched but the rest will stay the same.
我会用你想要的外观自己做一个 9-patch 图像,保持比例和除图标之外的所有内容,所以你可以自己添加它(并重用背景),drawableLeft
属性为EditText
. 这样只有图像的中间部分会被拉伸,但其余部分将保持不变。