在Android中以编程方式使TextView可滚动

4

我希望能够通过编程方式创建可滚动的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;
}
2个回答

0

你可以将 TextView 包裹在一个 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);

0

更改为:

valueTV.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

如果你使用 WRAP_CONTENTTextView 将会无限增加高度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接