如何在文本区域中添加图像和文本?

时间:2014-08-13 19:39:24

标签: android

我正在编写一个应用程序来保存一些文本数据以及嵌入在我的服务器上的文本内的图像。我的问题是如何将图像和文本添加到Android应用程序的文本区域?记住用户将添加图像并写入文本区域中的文本,所以我们不能硬编码。

4 个答案:

答案 0 :(得分:0)

您可以将图像设置为背景,也可以使用相对布局,并将textview放在图像视图的顶部。

<EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/your_pic"
  android:hint="@string/your_hint_text" />

<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:hint="@string/your_hint_text" />
</RelativeLayout>

答案 1 :(得分:0)

您应该考虑使用布局。

如果要将图像设置为背景,可以使用FrameLayout

答案 2 :(得分:0)

当它是一个EditText并且用户可以在那里输入时,它会做很多工作,但它肯定会起作用。阅读ImageSpan - 它会做你想做的事。您只需要创建一个Spannable:

Spannable span = Spannable.Factory.getInstance().newSpannable(text);

稍后将ImageSpan设置为:

span.setSpan(new ImageSpan(drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

并为EditText设置此范围

editText.setText(span);

我认为你应该将你的图像添加为<heart> <smile> <sad>等。然后制作一个简短的方法来找到这样的字符串,创建适当的跨度并在EditText中设置这个范围。

答案 3 :(得分:0)

我对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>
相关问题