回答
如果是這樣的:
是你在說些什麼,那麼你只需要可以設定在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