[英]How to set background color and underline for an ImageSpan
如上所示,我已將圖像插入到EditText
,其前后帶有文本。 因此,我使用了ImageSpan
。
我希望更改背景顏色並動態給文本加下划線,但是我發現它對圖像沒有影響。
我該怎么做才能使ImageSpan
在附近文本上顯示相同的效果?
對於背景色,我提供了部分解決方案。 我發現做到這一點的唯一方法(除了編寫自己的自定義span類)是將可繪制圖像放入LayerList
頂部的LayerList
,該GradientDrawable
是背景色。 在代碼中,可能是這樣的:
// Assume that d is the image drawable and bgSpan is the background color span.
// Then instead of doing
// ImageSpan imgSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
// you do the following:
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(bgSpan.getBackgroundColor());
LayerDrawable ld = new LayerDrawable(new Drawable[] {gd, d});
ld.setBounds(d.getBounds());
ImageSpan imgSpan = new ImageSpan(ld, ImageSpan.ALIGN_BASELINE);
您可能可以使用相同的技巧來模擬下划線(通過向圖層列表中添加另一個可繪制對象)。 但是,我尚未對此進行試驗,因此無法提供具體建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.