【问题标题】:How to set background color and underline for an ImageSpan如何为 ImageSpan 设置背景颜色和下划线
【发布时间】:2013-05-11 01:45:18
【问题描述】:

如上所示,我在EditText 中插入了一张图片,前后都有文字。所以,我使用了ImageSpan

我想动态改变背景颜色和给文字加下划线,但是发现对图片没有效果。

我应该怎么做才能使ImageSpan 显示与邻近文本相同的效果?

【问题讨论】:

  • 在 textchanged 方法上使用 textwatcher
  • 谢谢,但这不是我需要的...输入所有文本后如何更改 ImageSpan 的样式?

标签: java android image html


【解决方案1】:

我有一个部分解决方案可以提供 - 背景颜色。我发现这样做的唯一方法(除了编写我自己的自定义跨度类)是将图像可绘制到 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);

您也许可以使用相同的技巧来模拟下划线(通过向图层列表添加另一个可绘制对象)。但是,我没有对此进行过实验,也无法提供具体建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2010-12-11
    • 2020-12-14
    • 1970-01-01
    • 2019-02-28
    • 2013-04-26
    相关资源
    最近更新 更多