簡體   English   中英

TextView 中的內嵌圖像

[英]Inline images in a TextView

我正在嘗試將內聯圖像放入 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);

它可以工作(見下圖),但我需要圖像垂直居中。 我怎樣才能做到這一點?

在此處輸入圖像描述

謝謝

而不是使用 HTML,您可以使用

labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);

並使用setCompoundDrawablePadding玩一點填充

為什么不使用android:drawableRight

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM