在TextView的要点被切断(Bullet points in textview are cut

2019-10-19 18:42发布

我有一个TextView在一个应用程序,它的文本由布局中的硬编码字符串资源设置。 为了获得在项目符号列表TextView ,我用的(非官方?)的支持<li>元素。 这将创建正确缩进子弹,根据需要,但子弹本身被轻微地切除,因为你可以看到最左边:

我曾尝试加入左填充到这些,但它没有做任何的裁剪边缘 - 刚搬到整件事向内。

  1. 有没有简单的解决方案来解决呢?
  2. 哪里该项目符号列表中的资源生活?

Answer 1:

老问题,但大家有没有发现晚:

我发现内置的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);
        }
    }
}


Answer 2:

尝试使用Unicode字符•(统一2022)? 看起来你可以将其粘贴到XML。

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



Answer 3:

               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;
            }

        }


文章来源: Bullet points in textview are cut off