Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在文本中插入可绘图项_Android_User Interface_Textview_Imageview - Fatal编程技术网

Android 如何在文本中插入可绘图项

Android 如何在文本中插入可绘图项,android,user-interface,textview,imageview,Android,User Interface,Textview,Imageview,我想在文本视图内容的某些位置插入小图片,例如箭头图标 下面的照片正是我想要的: 显然,最简单的解决方案是在小型ImageView对象的任一侧使用多个TextView。但这种方法是不可扩展的 我很想知道是否有人用一些简单而聪明的技巧克服了这个问题。 (可能需要HTML或外部库的帮助) 非常感谢任何有效的解决方案。您可以创建SpannableString并将任何对象添加到字符串中 TextView textView = (TextView) findViewById(R.id.textView);

我想在
文本视图
内容的某些位置插入小图片,例如箭头图标

下面的照片正是我想要的:

显然,最简单的解决方案是在小型
ImageView
对象的任一侧使用多个
TextView
。但这种方法是不可扩展的

我很想知道是否有人用一些简单而聪明的技巧克服了这个问题。 (可能需要HTML或外部库的帮助)


非常感谢任何有效的解决方案。

您可以创建SpannableString并将任何对象添加到字符串中

TextView textView = (TextView) findViewById(R.id.textView);

ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_launcher);
SpannableString spannableString = new SpannableString(textView.getText());

int start = 3;
int end = 4;
int flag = 0;
spannableString.setSpan(imageSpan, start, end, flag);

textView.setText(spannableString);
一个选项是使用而不是
TextView
。这还允许您显示资产文件夹中的图像

有关更多详细信息,请参阅。

不久前,有人提出了一个很棒的解决方案。我刚刚稍微调整了一下这个,这样图像的大小总是和线条一样高。因此,基本上你的图标将按文本大小缩放

步骤1-创建新视图 创建一个扩展
TextView

public class TextViewWithImages extends TextView {

    public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }
    public TextViewWithImages(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public TextViewWithImages(Context context) {
        super(context);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        Spannable s = getTextWithImages(getContext(), text, this.getLineHeight());
        super.setText(s, BufferType.SPANNABLE);
    }

    private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();

    private static boolean addImages(Context context, Spannable spannable, float height) {
        Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
        boolean hasChanges = false;

        Matcher matcher = refImg.matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end()
                        ) {
                    spannable.removeSpan(span);
                } else {
                    set = false;
                    break;
                }
            }
            String resName = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
            int id = context.getResources().getIdentifier(resName, "drawable", context.getPackageName());
            Drawable mDrawable = context.getResources().getDrawable(id);
            mDrawable.setBounds(0, 0, (int)height, (int)height);
            if (set) {
                hasChanges = true;
                spannable.setSpan(  new ImageSpan(mDrawable),
                        matcher.start(),
                        matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                );
            }
        }

        return hasChanges;
    }
    private static Spannable getTextWithImages(Context context, CharSequence text, float height) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addImages(context, spannable, height);
        return spannable;
    }
}
输出:

如前所述,它将根据您的
textSize
进行缩放:


正如最初所说的,这篇文章的大部分内容都取自《答案》。我认为这应该作为一个库发布,因为在文本中包含图像是一个常见的用例看一看这个项目:@Egor:这很好,但是只在图像后面提供文本,所以在上面图片的情况下,我必须使用并对齐5个IconTextView对象,再加上1个TextView。在哪一点上会发生此错误?真不敢相信它会立即出现,为了正确地帮助您,您也应该添加当前的布局。我已经包含了布局,只有当我将两行以上的文本设置到TextViewWithImage对象时才会出现问题。这是个好主意,尽管我宁愿使用drawable文件夹而不是资源,因为我需要为不同的地区和屏幕高效地使用不同的图像。不管怎样,我很感激你的解决方案。你能详细说明一下吗?我有5张图片要输入文本,而不仅仅是一张。您的解决方案是否可以按比例放大以满足我所述的需要?为要插入的每个图像的SpanAbleString添加一个新的ImageSpan。如何从网络加载ImageSpan?谢谢,这很漂亮,得试试!我完全听到了你关于图书馆项目的声音。你知道这次崩溃吗?不幸的是,我不能重现这个错误。我刚刚在我的
文本视图中设置了20行图像,没有发生崩溃。您的设备运行在哪一个Android版本?4.2.2,我需要插入最多5个可绘图页。@j2emanue好的,可绘图页的大小设置为线条本身的高度(在添加图像之前)。这可能是一个选项,以减少可绘制的#立根方法中的尺寸。
<com.stacko.examples.TextViewWithImages
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:text="@string/my_string_with_icons" />
<string name="my_string_with_icons">The [img src=ic_action_trash/] is used to delete an item while the [img src=ic_action_edit/] is to edit one.</string>