Android 如何在 EditText 上添加图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3703283/
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
How can I add an image on EditText
提问by sivaraj
I want to dynamically add image in EditText
. Is it possible?
我想在EditText
. 是否可以?
if anyone knows please give sample code for that.
如果有人知道,请提供示例代码。
回答by CaseyB
If something like this:
如果是这样的:
is what you're talking about, then you just need to either set the Drawable{Right | Left | Top | Bottom}
property in the xml, or call the corresponding java command.
就是你所说的,那么你只需要Drawable{Right | Left | Top | Bottom}
在xml中设置属性,或者调用相应的java命令。
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);
回答by Ramesh Akula
Using the frame layout it is very easy to overcome this.Here another advantage is that you can give click events for buttons.if you set icons using setCompoundDrawables, you cant give the click events.I have implemented in my project that search bar has delete and search icons.
使用框架布局很容易克服这个问题。这里的另一个优点是您可以为按钮提供点击事件。如果您使用 setCompoundDrawables 设置图标,则无法提供点击事件。我在我的项目中实现了搜索栏已删除和搜索图标。
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/search_bar"
android:drawablePadding="8dp"
android:paddingLeft="30dp"
android:paddingRight="10dp"
android:inputType="text"
android:maxLines="1">
<requestFocus />
</EditText>
<Button
android:id="@+id/searchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_margin="10dp"
android:background="@drawable/icon_magnify" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_margin="8dp"
android:background="@drawable/icon_remove" />
</FrameLayout>
回答by Ging3r
using android:drawableRight
or android:drawableLeft
(depend where you prefer align image)
使用android:drawableRight
或android:drawableLeft
(取决于您喜欢对齐图像的位置)
<EditText
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/searchedTxt"
android:width="200dp"
android:layout_alignParentRight="true"
android:drawableRight="@android:drawable/ic_menu_search"
android:drawablePadding="@dimen/dimen_10dp"
/>
回答by Buda Gavril
you can also try this
你也可以试试这个
SpannableString ss = new SpannableString("abc\n");
Drawable d = img.getDrawable();
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
editT.setText(ss);
回答by Mihir Palkhiwala
You can use setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom).
您可以使用setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)。
EditText text = (EditText) findViewById(R.id.edtTest);
text.setText("Test");
text.setCompoundDrawablesWithIntrinsicBounds(null, null,
getResources().getDrawable(R.drawable.myDrawable, null), null);
回答by sami boussacsou
You can add an image to your EditText
through android:background="@drawable/img"
.
您可以将图像添加到您的EditText
通过android:background="@drawable/img"
。
If you want to modify the style by using nine patch or else, but if you want to add a small image in the left of your EditText
consider using android:drawableRight="@drawable/icon"
.
如果您想通过使用九个补丁或其他方式修改样式,但如果您想在左侧添加一个小图像,请EditText
考虑使用android:drawableRight="@drawable/icon"
.
回答by Hammad Hasan
extend the edittext class and do code as u want it to be
扩展edittext类并按照您的意愿执行代码
public void setImage(String imageResource) {
int position = Selection.getSelectionStart(MyEditText.this
.getText());
Spanned e = Html.fromHtml("<img src=\"" + imageResource + "\">",
imageGetter, null);
MyEditText.this.getText().insert(position, e);
}
回答by Amitay Feder
You can use:
您可以使用:
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0);
as suggested here:
Calling setCompoundDrawables() doesn't display the Compound Drawable
回答by Ajay Sivan
Create an instance of the EditText
创建一个实例 EditText
EditText editText = (EditText) findViewById(R.id.EditText1);
Then create a drawable instance of the image
然后创建图像的可绘制实例
Drawable image = getResources().getDrawable(R.drawable.ic_warning); // API 21 and below.
OR
或者
Drawable image = getDrawable(R.drawable.ic_warning); // API 22 and above.
Then set the image by calling setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
然后通过调用设置图像 setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null);
The above line will set the image in the right side of the EditText
上面的行将在右侧设置图像 EditText
回答by Hammad Hasan
You can try this if you are in adapter class
如果你在适配器类中,你可以试试这个
holder.myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
_context.getResources().getDrawable(R.drawable.ic_launcher),
null);
and You can try this if you are in activity class
如果你在活动课上, 你可以试试这个
myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
getResources().getDrawable(R.drawable.ic_launcher),
null);