java - TextView 中的内嵌图像

标签 java android html

我正在尝试将内联图像放入 TextView 中。这是我的代码:

ImageGetter imageGetter = new ImageGetter() {
    public Drawable getDrawable(String source) {
        Drawable d = ctx.getResources().getDrawable(Integer.parseInt(source));
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
};
CharSequence votesString="124 votes";
votesString=Html.fromHtml(votesString
           +"(<img src='" + R.drawable.icon_upvotes + "'/>)", imageGetter, null);
labelVotes.setText(votesString);

它可以工作(见下图),但我需要图像垂直居中。我怎样才能做到这一点?

enter image description here

谢谢

最佳答案

您可以使用

而不是使用 HTML
labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);

并使用 setCompoundDrawablePadding 进行一点填充

关于java - TextView 中的内嵌图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21848957/

相关文章:

java - 这两个 block 中的哪一个会捕获更多异常,Exception 或 RuntimeException

java - 强制 Java Android Socket 立即发送数据

android - 在 Android 上使用 MediaMuxer 录制音频和视频

javascript - 使用 angularJS 使所有字段在点击时可编辑

Javascript 层次结构/树创建器

java - Android 应用程序中的云驱动器使用

java - 在 Java 中将 Epoch 秒转换为日期和时间格式

android - 在androidx.fragment.app.Fragment中,setUserVisibleHint()被弃用了,并且没有被执行,为什么?

c# - 循环到 ListView 元素

html - 让 <DIV> 垂直拥抱文本内容