繁体   English   中英

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

[英]Making TextView Scrollable in Android programmatically

我想以编程方式制作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_CONTENTTextView将“无限期”增加高度。

您可以将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