添加BulletSpan时,EditText不会增加宽度

时间:2014-11-27 13:51:25

标签: android android-layout android-edittext width bulletedlist

将BulletSpan添加到EditText时,编辑文本的大小不会随着项目符号添加的前导空格而增加。这导致文本的包装或滚动不在视线范围内。

Sofar我无法强制EditText正确地重新测量其宽度。

不幸的是,我没有足够的声誉来说明这种情况。让我试着描述一下情况。

  1. 将BulletSpan添加到EditText的基础可编辑时,生成的较大段落会在右边缘后面滚动。

    |0123456789|  -> | • 0123456|789  
    

    最后一个字符在右边缘后面滚动。

  2. 当更改的editable分配给EditText时,字段的宽度保持不变,更宽的文本换行。

    |0123456789| -> | • 0123456|
                    |   789    |
    

    在这两种情况下,EditText都不会补偿前导空格的宽度。

  3. 在段落末尾添加额外字符

    | • 0123456789xxx|
    |   xxx          |
    

    在EditText中添加字符时,字段的大小会按预期增加,但不会考虑子弹及其边距的额外空间并保持包装。

  4. 有人问过有关BulletSpans的几个问题,但之前没有这个奇怪的行为。 有两天,我在xml中尝试了很多布局设置,但代码中的很多选项都没有用。

    有人有建议吗?

    代码:MainActivity.java

            mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean showBullet) {
                final Editable editable = mEditText1.getText();
    
                if (showBullet) {
                    // Add bullet
                    editable.setSpan(new BulletSpan(50), 0, editable.length(), Spannable.SPAN_PARAGRAPH);
                } else {
                    // Remove bullet
                    BulletSpan[] spans = editable.getSpans(0, editable.length(), BulletSpan.class);
                    if (spans.length > 0) {
                        editable.removeSpan(spans[0]);
                    }
                }
                // See what happens when assigning the editable to an other TextView
                mEditText2.setText(editable);
            }
        });
    

    布局:activity_main.xml (部分)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"> 
    
    <!-- ToggleButton and TextView removed -->
    
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:inputType="textMultiLine"
        android:text="0123456789" />
    

1 个答案:

答案 0 :(得分:0)

尝试覆盖BulletSpan方法并更改其实现:

public class MyBulletSpan extends BulletSpan {

    @Override
    public int getLeadingMargin(boolean first) {
        return super.getLeadingMargin(first);
    }

    @Override
    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) {
        super.drawLeadingMargin(c, p, x, dir, top, baseline, bottom, text, start, end, first, l);
    }
}