Android 如何在文本区域中同时添加图像和文本?
我正在编写一个应用程序来保存一些文本数据以及嵌入在我的服务器文本中的图像。我的问题是如何在Android应用程序中向文本区域添加图像和文本?请记住,用户将添加图像并在文本区域中写入文本,因此我们无法对其进行硬编码。您可以将图像设置为背景或可以使用相对布局并将文本视图置于imageview的顶部Android 如何在文本区域中同时添加图像和文本?,android,Android,我正在编写一个应用程序来保存一些文本数据以及嵌入在我的服务器文本中的图像。我的问题是如何在Android应用程序中向文本区域添加图像和文本?请记住,用户将添加图像并在文本区域中写入文本,因此我们无法对其进行硬编码。您可以将图像设置为背景或可以使用相对布局并将文本视图置于imageview的顶部 <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:ba
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/your_pic"
android:hint="@string/your_hint_text" />
或
< /代码> 您应该考虑使用布局。
如果您想将图像设置为背景,您可以使用FrameLayout
当它是编辑文本时,需要做大量的工作,用户可以在那里键入,但肯定可以。阅读-它会做你想做的事。您只需使用以下内容创建一个Spanable:
Spannable span = Spannable.Factory.getInstance().newSpannable(text);
稍后将ImageSpan设置为:
span.setSpan(new ImageSpan(drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
并为编辑文本设置此跨度
editText.setText(span);
我认为您应该将图像添加为
等,并制作一个简短的方法来查找此类字符串,创建适当的跨距,并在编辑文本中设置此跨距。我对Cory Roy的答案稍加修改,就同意了。因为你说用户可以输入文本。不要使用TextView
而使用EditText
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_pic" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_text"
android:hint=@"string/your_hint_text" />
</RelativeLayout>
他们将如何将图像添加到文本区域?您能否描述您的用户将如何与您的应用程序交互?也许可以提供你想要屏幕外观的模型。如果他们分别上传图像和编写文本,你应该有一个带有ImageView和EditText的布局。根据Rani的反馈,我切换到EditText
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_pic" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_text"
android:hint=@"string/your_hint_text" />
</RelativeLayout>