Reset Android textview maxlines

前端 未结 2 862
故里飘歌
故里飘歌 2021-01-31 07:26

I want to make a TextView that is collapsable by user\'s touch. When the TextView collapsed, I set textView.setMaxLines(4);. How to I clear this state in my expand

2条回答
  •  心在旅途
    2021-01-31 07:55

    Actually, the way android platform does that is by setting the MaxLine to Integer.MAX_VALUE.

    textView.setMaxLines(Integer.MAX_VALUE);
    

    also, if you are using Ellipsize, don't forget to set to null.

    textView.setEllipsize(null);
    

    just check how the android framework do just that ;) watch the setMaxLines(Integer.MAX_VALUE);

    private void applySingleLine(boolean singleLine, boolean applyTransformation) {
        mSingleLine = singleLine;
        if (singleLine) {
            setLines(1);
            setHorizontallyScrolling(true);
            if (applyTransformation) {
                setTransformationMethod(SingleLineTransformationMethod.getInstance());
            }
           } else {
                setMaxLines(Integer.MAX_VALUE);
                setHorizontallyScrolling(false);
                if (applyTransformation) {
                     setTransformationMethod(null);
            }
           }
         }
    

    You can find this in the source code of Android Open Source Project (AOSP)

    https://source.android.com/source/downloading

    If you do not want to download the source, you can view the source on a mirror like this one at github.

    https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java

提交回复
热议问题