簡體   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