【问题标题】:Making TextView Scrollable in Android programmatically以编程方式在 Android 中使 TextView 可滚动
【发布时间】:2015-11-14 10:25:16
【问题描述】:

我想以编程方式制作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;
}

【问题讨论】:

    标签: android scroll textview


    【解决方案1】:

    改为:

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

    如果您使用WRAP_CONTENTTextView 将“无限”地增加高度。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 2016-01-30
        • 2017-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多