【发布时间】:2011-04-11 19:45:51
【问题描述】:
我想在EditText 中动态添加图片。可能吗?
如果有人知道,请提供示例代码。
【问题讨论】:
-
定义“在
EditText添加图片”
标签: android image android-edittext
我想在EditText 中动态添加图片。可能吗?
如果有人知道,请提供示例代码。
【问题讨论】:
EditText添加图片”
标签: android image android-edittext
如果是这样的:
就是你说的,那你要么在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);
【讨论】:
使用框架布局很容易克服这个问题。这里的另一个优点是您可以为按钮提供点击事件。如果您使用 setCompoundDrawables 设置图标,则无法提供点击事件。我在我的项目中实现了搜索栏有删除和搜索图标。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/search"
android:layout_width="match_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>
【讨论】:
android:inputType="text" android:maxLines="1"并从EditText中删除singleLine
使用android:drawableRight 或android:drawableLeft
(取决于你喜欢在哪里对齐图像)
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/searchedTxt"
android:width="200dp"
android:layout_alignParentRight="true"
android:drawableRight="@android:drawable/ic_menu_search"
android:drawablePadding="@dimen/dimen_10dp"
/>
【讨论】:
你也可以试试这个
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);
【讨论】:
setSpan中的3参数从何而来?
thiss.getResources().getDrawable(R.drawable.friends)
您可以使用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);
【讨论】:
您可以通过android:background="@drawable/img" 将图像添加到您的EditText。
如果您想使用九个补丁或其他方式修改样式,但如果您想在EditText 的左侧添加一个小图像,请考虑使用android:drawableRight="@drawable/icon"。
【讨论】:
扩展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);
}
【讨论】:
你可以使用:
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.
或
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的右侧
【讨论】:
如果你在适配器类中,你可以试试这个
holder.myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
_context.getResources().getDrawable(R.drawable.ic_launcher),
null);
和 如果你在活动课上可以试试这个
myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
getResources().getDrawable(R.drawable.ic_launcher),
null);
【讨论】: