簡體   English   中英

將圖像附加到TextView

[英]Append image to TextView

我已經嘗試了兩天,將圖像追加到TextView上,但是沒有用。 我已經針對Android 6 / API級別23進行了嘗試。ImageSpan,Html.fromHtml和其他方法無效。 那么是否可以在Android 6的TextView中的其他單詞之間附加圖像(位圖)?


編輯


這是XML:

<TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:text="------"
            android:gravity="bottom|left"
            android:typeface="monospace"
            android:id="@+id/mainTextView1"
            android:textIsSelectable="true"/>

和圖像跨度代碼:

SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText()); ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mOutEditText.setText(ssb, BufferType.SPANNABLE);

在xml中添加圖片

<TextView
    android:drawableBottom="@drawable/ic_add_circle_black_24dp"
    android:drawableEnd="@drawable/ic_add_circle_black_24dp"
    android:drawableLeft="@drawable/ic_add_circle_black_24dp"
    android:drawablePadding="@drawable/ic_add_circle_black_24dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

編程

textView1.setCompoundDrawables(left,top,right,bottom);

或只剩下

textView1.setCompoundDrawables(left,null,null,null);

我現在找到了Api 21、22、23+的解決方案:

private void appendImage(Bitmap bmp)
{
    tv.setTransformationMethod(null);
    SpannableString ss = new SpannableString("  ");
    ss.setSpan(new ImageSpan(bmp, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    tv.append(ss);
}

在XML中:

android:textAllCaps="false"

錯誤是在api級別21+中,您必須將TransformationMethod設置為null。

如果要設置TextView的背景,請使用以下方法:

txt.setBackgroundResource(int rsid);
txt.setBakgroundDrawable(Drawable object);

如果是追加,則表示您要將其添加到TextView的末尾,然后對圖像使用ImageView。 或者,您可以將TextView覆蓋在ImageView中

無法在TextView的文本之間添加圖像。 您可以在圖像的開始和結束處添加一個圖像。 這是用

android:drawableLeft

暫無
暫無

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

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