以编程方式在Android中制作TextView Scrollable
作者:互联网
我想以编程方式创建textView并使它们可滚动.我一次又一次地调用这个方法来获取textView并将其添加到linearLayout.
TextView textView = addTextView(contents.paragraphs.get(i),false);
linearLayout.addView(textView);
但是,它根本不可滚动.方法是:
private TextView addTextView(String text,boolean type)
{
TextView valueTV = new TextView(this);
valueTV.setText(text);
valueTV.setTextColor(getResources().getColor(R.color.colorTextPrimary));
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
valueTV.setMaxLines(1000);
valueTV.setVerticalScrollBarEnabled(true);
valueTV.setMovementMethod(new ScrollingMovementMethod());
return valueTV;
}
解决方法:
改成:
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
如果使用WRAP_CONTENT,TextView将无限期地增加高度.
标签:android,android-textview 来源: https://codeday.me/bug/20190711/1433868.html