我想以编程方式制作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;
}
答案 0 :(得分:0)
更改为:
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
如果您使用WRAP_CONTENT
,TextView
会无限期地增加身高'
答案 1 :(得分:0)
您可以将其包装到ScrollView
中 ViewGroup linearLayout = findViewById(R.id.linear_layout);
TextView textView = addTextView(contents.paragraphs.get(i),false);
ScrollView scroller = new ScrollView(getApplicationContext());
scroller.addView(textView);
linearLayout.addView(scroller);