以编程方式在Android中制作TextView Scrollable

时间:2015-08-20 16:52:31

标签: android textview

我想以编程方式制作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 :(得分:0)

更改为:

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

如果您使用WRAP_CONTENTTextView会无限期地增加身高'

答案 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);