简体   繁体   English

如何获得可扩展的textview宽度以将其修剪长度设置为1行

[英]how to get expandable textview width to set its trim length to 1 line

i'm using expandable textview to display some part of the text and when user clicks on this textview then user can see whole String of that text for that i'm using this example but the problem is the trim length of the expandable textview its set to fixed, but i want to set the trim length dynamic based on screen size with only one line, when i use trim_length = 200 the text displayed is of 3 lines, here is my code... 我正在使用可扩展文本视图显示文本的某些部分,并且当用户单击该文本视图时,用户可以看到该文本的整个字符串,我正在使用此示例,但是问题是可扩展文本视图的修剪长度固定,但是我想根据屏幕尺寸仅用一行设置动态调整长度,当我使用trim_length = 200时,显示的文本为3行,这是我的代码...

ExpandableTextView.java ExpandableTextView.java

public class ExpandableTextView extends TextView {
    private static final int DEFAULT_TRIM_LENGTH = 200;
    private static final String ELLIPSIS = ".....";

    private CharSequence originalText;
    private CharSequence trimmedText;
    private BufferType bufferType;
    private boolean trim = true;
    private int trimLength;

    public ExpandableTextView(Context context) {
        this(context, null);
    }

    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
        typedArray.recycle();

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                trim = !trim;
                setText();
                requestFocusFromTouch();
            }
        });
    }

    private void setText() {
        super.setText(getDisplayableText(), bufferType);
    }

    private CharSequence getDisplayableText() {
        return trim ? trimmedText : originalText;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        trimmedText = getTrimmedText(text);
        bufferType = type;
        setText();
    }

    private CharSequence getTrimmedText(CharSequence text) {
        if (originalText != null && originalText.length() > trimLength) {
            return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);
        } else {
            return originalText;
        }
    }

    public CharSequence getOriginalText() {
        return originalText;
    }

    public void setTrimLength(int trimLength) {
        this.trimLength = trimLength;
        trimmedText = getTrimmedText(originalText);
        setText();
    }

    public int getTrimLength() {
        return trimLength;
    }
}

ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(R.id.lorem_ipsum); ExpandableTextView expandableTextView =(ExpandableTextView)findViewById(R.id.lorem_ipsum); expandableTextView.setText(yourText); expandableTextView.setText(yourText);

you can do it like this 你可以这样

public class ExpandableTextView extends TextView
{
    // copy off TextView.LINES
    private static final int MAXMODE_LINES = 1;
//    private OnExpandListener onExpandListener;
    private final int maxLines;
    private boolean expanded;


    public ExpandableTextView(final Context context)
    {
        this(context, null);
    }

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

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

        // read attributes
        final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView, defStyle, 0);
//        this.animationDuration = attributes.getInt(R.styleable.ExpandableTextView_trimLength, 200);
        attributes.recycle();

        // keep the original value of maxLines
        this.maxLines = this.getMaxLines();


    }

    @Override
    public int getMaxLines()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            return super.getMaxLines();
        }

        try
        {
            final Field mMaxMode = TextView.class.getField("mMaxMode");
            mMaxMode.setAccessible(true);
            final Field mMaximum = TextView.class.getField("mMaximum");
            mMaximum.setAccessible(true);

            final int mMaxModeValue = (int) mMaxMode.get(this);
            final int mMaximumValue = (int) mMaximum.get(this);

            return mMaxModeValue == MAXMODE_LINES ? mMaximumValue : -1;
        }
        catch (final Exception e)
        {
            return -1;
        }
    }

    public boolean toggle()
    {
        return this.expanded
                ? this.collapse()
                : this.expand();
    }


    public boolean expand()
    {
        if (!this.expanded && this.maxLines >= 0)
        {

            // set maxLines to MAX Integer, so we can calculate the expanded height
            this.setMaxLines(Integer.MAX_VALUE);


            // keep track of current status
            ExpandableTextView.this.expanded = true;


            return true;
        }

        return false;
    }


    public boolean collapse()
    {
        if (this.expanded && this.maxLines >= 0)
        {  

            ExpandableTextView.this.setMaxLines(ExpandableTextView.this.maxLines);
            // keep track of current status
            ExpandableTextView.this.expanded = false;


            return true;
        }

        return false;
    }
}

activity.xml activity.xml

<com.yourpackagename.ExpandableTextView
            android:id="@+id/expandableTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
     />

activity.java activity.java

ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView);
expandableTextView.setOnClickListener(new View.OnClickListener()
        {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void onClick(final View v)
            {
                expandableTextView.toggle();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM