Пули в текстовом представлении обрезаются

У меня есть TextView в приложении, текст которого задается жестко закодированным строковым ресурсом в макете. Для того, чтобы получить маркированный список в TextViewЯ использовал (неофициальную?) Поддержку для <li> элемент. При этом создаются пули с правильными отступами, но при этом левый край самих пуль слегка обрезается, как вы можете видеть:

на этом изображении.

Я попытался добавить к ним левый отступ, но он ничего не сделал с обрезанным краем - просто переместил все это внутрь.

  1. Есть ли простое решение, чтобы решить эту проблему?
  2. Где находится ресурс для этого маркированного списка?

3 ответа

Решение

Старый вопрос, но для всех, кто нашел это поздно:

Я обнаружил, что встроенный класс BulletSpan содержит ошибки от ранних версий Android вплоть до зефира:

  • Радиус пули и ширина зазора не зависят от dp
  • Пули иногда обрезаются (должно быть + BULLET_RADIUS, а не * BULLET_RADIUS)

Предупреждение: я видел несколько пользовательских классов BulletSpan, которые реализуют ParcelableSpan как внутренний класс. Это приведет к сбоям и не предназначено для внешнего использования.

Вот мой BulletSpanCompat:

public class BulletSpanCompat implements LeadingMarginSpan {
    private final int mGapWidth;
    private final boolean mWantColor;
    private final int mColor;

    private static final int BULLET_RADIUS = MaterialDesignUtils.dpToPx(1.5f);
    private static Path sBulletPath = null;
    public static final int STANDARD_GAP_WIDTH = MaterialDesignUtils.dpToPx(8);

    public BulletSpanCompat() {
        mGapWidth = STANDARD_GAP_WIDTH;
        mWantColor = false;
        mColor = 0;
    }

    public BulletSpanCompat(int gapWidth) {
        mGapWidth = gapWidth;
        mWantColor = false;
        mColor = 0;
    }

    public BulletSpanCompat(int gapWidth, int color) {
        mGapWidth = gapWidth;
        mWantColor = true;
        mColor = color;
    }

    public BulletSpanCompat(Parcel src) {
        mGapWidth = src.readInt();
        mWantColor = src.readInt() != 0;
        mColor = src.readInt();
    }

    public int getLeadingMargin(boolean first) {
        return 2 * BULLET_RADIUS + mGapWidth;
    }
    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
                                  int top, int baseline, int bottom,
                                  CharSequence text, int start, int end,
                                  boolean first, Layout l) {
        if (((Spanned) text).getSpanStart(this) == start) {
            Paint.Style style = p.getStyle();
            int oldcolor = 0;

            if (mWantColor) {
                oldcolor = p.getColor();
                p.setColor(mColor);
            }

            p.setStyle(Paint.Style.FILL);

            if (c.isHardwareAccelerated()) {
                if (sBulletPath == null) {
                    sBulletPath = new Path();
                    // Bullet is slightly better to avoid aliasing artifacts on mdpi devices.
                    sBulletPath.addCircle(0.0f, 0.0f, 1.2f + BULLET_RADIUS, Path.Direction.CW);
                }

                c.save();
                c.translate(x + dir + BULLET_RADIUS, (top + bottom) / 2.0f);
                c.drawPath(sBulletPath, p);
                c.restore();
            } else {
                c.drawCircle(x + dir + BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p);
            }

            if (mWantColor) {
                p.setColor(oldcolor);
            }

            p.setStyle(style);
        }
    }
}
               Please use below code:-

             <CustomBulletTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Point1" />
            <CustomBulletTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Point2" />

           /* CustomBulletTextView.java */
            public class CustomBulletTextView extends TextView {
                public CustomBulletTextView(Context context) {
                    super(context);
                    addBullet();
                }

                public CustomBulletTextView(Context context, AttributeSet attrs) {
                    super(context, attrs);
                    addBullet();
                }

                public CustomBulletTextView(Context context, AttributeSet attrs, int defStyleAttr) {
                    super(context, attrs, defStyleAttr);
                    addBullet();
                }

                public CustomBulletTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
                    super(context, attrs, defStyleAttr, defStyleRes);
                    addBullet();
                }

                private void addBullet() {
                    CharSequence text = getText();
                    if (TextUtils.isEmpty(text)) {
                        return;
                    }
                    SpannableString spannable = new SpannableString(text);
                    spannable.setSpan(new CustomBulletSpan(16), 0, text.length(), 0);
                    setText(spannable);
                }
            }

              /* CustomBulletSpan.java */
            public class CustomBulletSpan implements LeadingMarginSpan, ParcelableSpan {
            private final int mGapWidth;
            private final boolean mWantColor;
            private final int mColor;

            private static final int BULLET_RADIUS = 3;
            private static Path sBulletPath = null;
            public static final int STANDARD_GAP_WIDTH = 2;

            public CustomBulletSpan(int gapWidth) {
                mGapWidth = gapWidth;
                mWantColor = false;
                mColor = 0;
            }

            public int describeContents() {
                return 0;
            }

            public void writeToParcel(Parcel dest, int flags) {
                dest.writeInt(mGapWidth);
                dest.writeInt(mWantColor ? 1 : 0);
                dest.writeInt(mColor);
            }

            public int getLeadingMargin(boolean first) {
                return 2 * BULLET_RADIUS + mGapWidth;
            }

            public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
                    CharSequence text, int start, int end, boolean first, Layout l) {
                // Here I shifted the bullets to right by the given half bullet
                x += mGapWidth / 2;
                if (((Spanned) text).getSpanStart(this) == start) {
                    Paint.Style style = p.getStyle();
                    int oldcolor = 0;

                    if (mWantColor) {
                        oldcolor = p.getColor();
                        p.setColor(mColor);
                    }

                    p.setStyle(Paint.Style.FILL);

                    if (c.isHardwareAccelerated()) {
                        if (sBulletPath == null) {
                            sBulletPath = new Path();
                            // Bullet is slightly better to avoid aliasing artifacts on
                            // mdpi devices.
                            sBulletPath.addCircle(0.0f, 0.0f, 1.2f * BULLET_RADIUS, Direction.CW);
                        }

                        c.save();
                        c.translate(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f);
                        c.drawPath(sBulletPath, p);
                        c.restore();
                    } else {
                        c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p);
                    }

                    if (mWantColor) {
                        p.setColor(oldcolor);
                    }

                    p.setStyle(style);
                }
            }

            @Override
            public int getSpanTypeId() {
                return 0;
            }

        }

Попробуйте использовать символ Unicode • (Unicode 2022)? Похоже, вы можете просто вставить его в XML.

http://www.fileformat.info/info/unicode/char/2022/index.htm

Другие вопросы по тегам