简体   繁体   English

ScrollView清除TextView文本后停止滚动

[英]ScrollView stops scrolling after clearing TextView text

I have a TextView inside a ScrollView: 我在ScrollView中有一个TextView:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:scrollbars="vertical" 
    android:fillViewport="true">

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
     />

</ScrollView>

I append text to the TextView and after doing so, the scrolling works ok. 我将文本追加到TextView,然后这样做,滚动正常。 However, if I clear the text via 但是,如果我通过清除文本

textView.setText("");

and then append more text, the scrolling no longer works. 然后添加更多文本,滚动将不再起作用。

The java code I'm using is like this: 我正在使用的Java代码是这样的:

public class MainActivity extends Activity {
  protected TextView textView;

  protected void onCreate(Bundle savedInstanceState){    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textview);
    setClickListeners();
  }
  private void setClickListeners() {
    ((Button) findViewById(R.id.clear)).setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        textView.setText("");
      }
    });
  }
}

I would expect the scrolling to still work. 我希望滚动仍然可以。 Could anyone say why it doesn't? 谁能说为什么不呢?

You need to change the TextView's height to be "wrap_content" instead of "fill_parent" so that it expands to be as large as its content, not just as large as the parent. 您需要将TextView的高度更改为“ wrap_content”而不是“ fill_parent”,以使其扩展到与其内容一样大,而不仅仅是与父项一样大。 That way, the ScrollView will have something to scroll to. 这样,ScrollView将有一些内容可以滚动到。

I can only guess that rotating the screen causes the elements to resize themselves to fit their content causing the discrepancy you saw... 我只能猜测旋转屏幕会导致元素调整自身大小以适合其内容,从而导致您看到的差异...

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

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