15

我有一个Text View图像跨度内容。我已经ellipsize = "end"Text View. 在 ellipsize 之后,图像跨度出现在它之后。

4

3 回答 3

5

使用 iosched 的EllipsizedTextView类。在视图使用方法中设置文本

setText(CharSequence text, BufferType type) 

对于 Spannable,您可以使用

textView.setText(text, TextView.BufferType.SPANNABLE);

同样在您的 xml 文件中,您将需要定义android:ellipsizeandroid:maxLines或者您也可以以编程方式设置它们。

把代码放在这里试试。代码已修改,取自this question

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button button;
    private EllipsizedTextView textView;

    private static final String PATTERN = "\\[\\w+?\\]";
    private Pattern pattern;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.edittext);
        button = (Button) findViewById(R.id.btn);
        textView = (EllipsizedTextView) findViewById(R.id.text);
        //programmatic way of setting max lines and ellipsize
        textView.setMaxLines(2);
        textView.setEllipsize(TextUtils.TruncateAt.END);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insertImageSpan();
            }
        });

        pattern = Pattern.compile(PATTERN);



        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textView.setText(s, TextView.BufferType.SPANNABLE);
            }
        });
    }

    private void insertImageSpan(){
        insertTextInCurrentPosition(editText,"[face]");
        int select = editText.getSelectionStart();
        editText.setText(spanText(editText.getText()));
        editText.setSelection(select);
    }

    public CharSequence spanText(CharSequence text) {
        SpannableStringBuilder t = null;
        if(text instanceof SpannableStringBuilder){
            t = (SpannableStringBuilder) text;
        }else{
            t = new SpannableStringBuilder(text);
        }
        Matcher m = pattern.matcher(text);
        while (m.find()) {
            String mResult = m.group();
            String key = mResult.substring(1, mResult.length() - 1);

            ImageSpan[] spans = t.getSpans(m.start(),m.end(),ImageSpan.class);
            if(spans== null || spans.length==0 ){
                try{
                    ImageSpan span = new ImageSpan(this , R.drawable.ic_launcher);
                    t.setSpan(span , m.start() , m.end() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }catch (Exception e){
                    continue;
                }
            }

        }
        return t;
    }

    public static void insertTextInCurrentPosition(EditText tv, CharSequence str) {
        if (tv == null || TextUtils.isEmpty(str)) return;
        tv.getText().replace(tv.getSelectionStart() , tv.getSelectionEnd() , str , 0 , str.length());
    }
}

和 EllipsizedTextView

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * A simple {@link TextView} subclass that uses {@link TextUtils#ellipsize(CharSequence,
 * android.text.TextPaint, float, android.text.TextUtils.TruncateAt, boolean,
 * android.text.TextUtils.EllipsizeCallback)} to truncate the displayed text. Th
 */
public class EllipsizedTextView extends TextView {
    private static final int MAX_ELLIPSIZE_LINES = 100;

    private int mMaxLines;

    public EllipsizedTextView(Context context) {
        this(context, null, 0);
    }

    public EllipsizedTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EllipsizedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // Attribute initialization
        final TypedArray a = context.obtainStyledAttributes(attrs, new int[]{
                android.R.attr.maxLines
        }, defStyle, 0);

        mMaxLines = a.getInteger(0, 1);
        a.recycle();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        CharSequence newText = getWidth() == 0 || mMaxLines > MAX_ELLIPSIZE_LINES ? text :
                TextUtils.ellipsize(text, getPaint(), getWidth() * mMaxLines,
                        TextUtils.TruncateAt.END, false, null);
        super.setText(newText, type);
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        super.onSizeChanged(width, height, oldWidth, oldHeight);
        if (width > 0 && oldWidth != width) {
            setText(getText());
        }
    }

    @Override
    public void setMaxLines(int maxlines) {
        super.setMaxLines(maxlines);
        mMaxLines = maxlines;
    }
}

和activity_main

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/edittext" />

        <com.anirudha.experiment.EllipsizedTextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:singleLine="false"/>
    </RelativeLayout>
</ScrollView>
于 2016-04-05T07:24:10.780 回答
1

你应该重写 SpannedEllipsizer,你可以在这里找到完美的分辨率: https://github.com/lsjwzh/FastTextView/blob/master/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java

于 2017-10-31T13:40:18.390 回答
-3

尝试这个:

android:ellipsize="end"

android:singleLine="true"
于 2016-04-04T20:16:42.783 回答