回答
如果是这样的:
是你在说些什么,那么你只需要可以设定在XML中Drawable{Right | Left | Top | Bottom}
属性,或者调用相应的java命令。
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);
您可以通过android:background="@drawable/img"
图像添加到您的EditText
。
如果您想通过使用九个补丁或其他方式来修改样式,但是如果您想在EditText
的左侧添加一个小图像,请考虑使用android:drawableRight="@drawable/icon"
。
@Thanks sami,我想动态添加图像在编辑文本中, – sivaraj 2010-09-14 08:23:53
你也可以试试这个
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);
使用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"
/>
这个人应该得到一枚奖章。多谢,伙计! – 2016-01-28 22:01:17
在最新的android studio 2.2.3中,建议使用android:drawableEnd代替android:drawableRight – 2017-01-13 09:50:20
使用框架布局是很容易克服这一点。另一个好处是,你可以给按钮的点击事件。如果你设置图标使用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>
如何将点击事件添加到按钮? – Scorpion 2013-02-15 11:41:08
findViewById(R.id.delete).setOnClickListener(new View。OnClickListener(){ \t \t \t \t \t \t @Override \t \t \t公共无效的onClick(视图v){ \t \t \t \t // TODO自动生成方法存根 \t \t \t \t \t \t \t} \t \t}); – 2013-02-15 12:28:42
你能提供可绘制的图像! – ManishSB 2014-08-21 18:24:44
您可以使用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);
扩展的EditText类,做代码为u希望它是
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);
}
imageResource是什么?它会像/sdcard/images/image1.jpg那样的图像路径吗? – NarendraJi 2015-11-24 10:20:53
你可以试试这个,如果你是在适配器类
holder.myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
_context.getResources().getDrawable(R.drawable.ic_launcher),
null);
和 你可以试试这个,如果你在活动课
myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
getResources().getDrawable(R.drawable.ic_launcher),
null);
您可以使用:
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0);
的建议在这里:
Calling setCompoundDrawables() doesn't display the Compound Drawable
创建的EditText
EditText editText = (EditText) findViewById(R.id.EditText1);
实例
然后创建该图像的绘制实例
Drawable image = getResources().getDrawable(R.drawable.ic_warning); // API 21 and below.
OR
Drawable image = getDrawable(R.drawable.ic_warning); // API 22 and above.
然后通过调用setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null);
上述线将设置在图像中的右侧设置的图像EditText
- 1. 如何动态地在edittext上添加图像?
- 2. 如何在EditText上添加多个ImageSpans?
- 3. 如何在图像上添加水印?
- 4. 如何在图像上添加笔划?
- 5. 如何在gridview上添加图像?
- 6. 将文字图像添加到EditText
- 7. 在图像上添加图像
- 8. 如何将gif图像添加到Android中的TextView或EditText中
- 9. 在UIImage上添加图像
- 10. 如何在iPhone上的图像上按像素添加颜色?
- 11. 如何在360图像上添加文本和图像A帧
- 12. 如何在Android的EditText视图中添加图标?
- 13. 在按钮上添加图像图标?
- 14. 如何在ImageView中添加EditText
- 15. 如何在editText中添加数字
- 16. 如何在TextView中添加EditText?
- 17. 如何添加图像RadTreeNode
- 18. 如何添加图像?
- 19. 输入的图像URL在图像上的editText上查看
- 20. 如何在图像上添加颜色叠加层
- 21. 如何在某些事件上的textview上添加图像
- 22. 如何在缩略图图像上添加html元素
- 23. 如何在某些按钮上添加图像图标?
- 24. 如何在面板中添加图像?
- 25. C#:如何在listview中添加图像?
- 26. 如何在listview.subitem中添加图像
- 27. 如何在JTextPane中添加图像?
- 28. 如何在库中添加图像
- 29. 如何在Latex中添加jpg图像
- 30. 如何在TCPDF中添加图像
定义“在EditText中添加图像” – CommonsWare 2010-09-13 19:43:32