How to disable the TextView maxLines programmatically?

前端 未结 4 1493
栀梦
栀梦 2021-02-04 23:55

I\'m having a hard time reseting the maxLines attribute of a TextView programmatically.

Just tried setting to 0 and it doesn\'t wo

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 00:39

    The -1 should not crash your application. This actually what is used inside TextView by default:

    case com.android.internal.R.styleable.TextView_maxLines:
                setMaxLines(a.getInt(attr, -1));
                break;
    

    This piece of code shows, that when android:maxLines is not specified, then code uses -1 to set the value via setMaxLines() function.


    I also made test application to verify my conclusions. And it works fine without crashing:

    public class HelloWorld extends Activity
    {           
    
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {       
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity);
    
            TextView text = (TextView)findViewById(R.id.text);
            text.setMaxLines(-1);    
        }
    }
    

提交回复
热议问题