【问题标题】:How to create gmail-like recipient (user) custom view如何创建类似 gmail 的收件人(用户)自定义视图
【发布时间】:2014-01-13 10:53:22
【问题描述】:

我的目标是创建在 Gmail 和 Foursquare 中使用的类似徽章的视图,如下所示。

到目前为止,我已经创建了ReplacementSpan 来处理单个视图的前景和背景颜色。

public class SearchTagSpan extends ReplacementSpan {

    private int backgroundColor;
    private int forgroundColor;

    public SearchTagSpan() {
        backgroundColor = -1;
        forgroundColor = Color.BLACK;
    }

    public SearchTagSpan(int backgroundColor, int forgroundColor) {
        this.backgroundColor = backgroundColor;
        this.forgroundColor = forgroundColor;
    }

    @Override
    public void draw(
            Canvas canvas,
            CharSequence text, int start, int end,
            float x, int top, int y, int bottom,
            Paint paint) {
        RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
        paint.setColor(backgroundColor);
        canvas.drawRect(rect, paint);
        paint.setColor(forgroundColor);
        canvas.drawText(text, start, end, x, y, paint);
    }

    @Override
    public int getSize(
            Paint paint,
            CharSequence text, int start, int end,
            Paint.FontMetricsInt fm) {
        return Math.round(paint.measureText(text, start, end));
    }

    private float measureText(Paint paint, CharSequence text, int start, int end) {
        return paint.measureText(text, start, end);
    }
}

正确应用背景和前景色,但是当视图变为多行时,每个徽章的背景会拉伸以填充TextView 布局中指定的行间距。如果我删除行距,则第一行和第二行的背景会相互接触,如下图所示。

我的方向正确吗?有什么我错过的吗?

【问题讨论】:

    标签: android gmail android-custom-view


    【解决方案1】:

    【讨论】:

    • 这正是我想要的。我应该记得我在某个地方读到过这个。谢谢。
    【解决方案2】:

    为此,您需要 ActionBar。 这个支持+3.0以上

    如果您在低版本的android中需要相同的功能,那么您可以使用appCompat支持库来支持低版本的android。

    请参考..http://developer.android.com/guide/topics/ui/actionbar.html

    最好的示例教程在这里...http://www.androidhive.info/2013/11/android-working-with-action-bar/

    希望对你有帮助。

    【讨论】:

    • 问题是关于 TO: box 中的芯片而不是 ActionBar。对不起,如果我让你感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2017-08-24
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多