TextView相关的纯技术难题

read_act 2016-07-20 12:05:00


第一张是UI 想要的效果 , 第二张是我用Span 实现的, 问题是lineSpaceExtre 行间距也被画上背景色了, UI要求是行与行之前是白的, 怎么实现? 不胜感激
...全文
230 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2016-07-21
  • 打赏
  • 举报
回复
引用 4 楼 read_act 的回复:
经过测试 , ReplacementSpan 回调draw有回调一次,里面确实可以执行draw 操作来做自己想做的事, 但给的坐标好像没什么用处啊, 大神,讲详细点呗
那个是比较麻烦的,这有一种简单的,不过可能效果不好,代码如下

public class BackgroundColorWithoutLineHeightSpan implements LineBackgroundSpan {
    private int mBackgroundColor;
    private int lineSpaceing;
    private Rect mBgRect;

    public BackgroundColorWithoutLineHeightSpan(int backgroundColor, int lineSpace) {
        super();
        mBackgroundColor = backgroundColor;
        lineSpaceing = lineSpace;
        // Precreate rect for performance
        mBgRect = new Rect();
    }

    @Override
    public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) {
        final int textWidth = Math.round(p.measureText(text, start, end));
        final int paintColor = p.getColor();
        // Draw the background
        mBgRect.set(left,
                top,
                left + textWidth,
                bottom - lineSpaceing);
        p.setColor(mBackgroundColor);
        c.drawRect(mBgRect, p);
        p.setColor(paintColor);
    }
}
如果对中间的行不能铺满不满意的话,那就把right设置为屏幕的宽度,具体自己把握。
jklwan 2016-07-20
  • 打赏
  • 举报
回复
自己画背景,自定义一个Span继承ReplacementSpan,在draw方法中画背景。
lwlizhe 2016-07-20
  • 打赏
  • 举报
回复
这样?http://blog.csdn.net/centralperk/article/details/8674599 将里面的那个前景色改为背景色?
read_act 2016-07-20
  • 打赏
  • 举报
回复
就没有牛人提供个思路?
read_act 2016-07-20
  • 打赏
  • 举报
回复
引用 3 楼 jklwan 的回复:
自己画背景,自定义一个Span继承ReplacementSpan,在draw方法中画背景。
经过测试 , ReplacementSpan 回调draw有回调一次,里面确实可以执行draw 操作来做自己想做的事, 但给的坐标好像没什么用处啊, 大神,讲详细点呗

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧