简体   繁体   English

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

[英]Making TextView Scrollable in Android programmatically

I want to make textView programmatically and make them scrollable.我想以编程方式制作textView并使它们可滚动。 I am calling this method again and again to get textView and adding it to the linearLayout .我一次又一次地调用此方法得到textView并将其添加到linearLayout

TextView textView = addTextView(contents.paragraphs.get(i),false);
linearLayout.addView(textView);

But, it is not scrollable at all.但是,它根本不可滚动。 The method is:方法是:

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;
}

Change to:改成:

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

If you use WRAP_CONTENT the TextView will increase height 'indefinitely'.如果您使用WRAP_CONTENTTextView将“无限期”增加高度。

You can wrap the TextView into a ScrollView您可以将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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM