gpt4 book ai didi

java - TextView 不会滚动

转载 作者:行者123 更新时间:2023-11-29 15:43:26 24 4
gpt4 key购买 nike

我目前在一个 android Activity 中工作,我需要一个可滚动的动态 TextView 。我不断地向这个 View 添加新行,但是当我向它添加行时,它最多只会增加 4 行(我也设置了它的数字)然后它不会滚动。这是我的 xml 和相关的 java 代码:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:scrollbars="vertical"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

Java:

 protected void onCreate(Bundle savedInstanceState) {
...
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
tv3 = (TextView) findViewById(R.id.textView2);
tv3.setMovementMethod(new ScrollingMovementMethod());

最佳答案

除非您将其置于 ScrollView 中,否则它不会滚动。如果您指定 maxLines,它不会超过该值。因此,不要指定最大行数并向 ScrollView 添加高度。

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="120dp"> //or whatever height

<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_centerHorizontal="true" />

</ScrollView>

</FrameLayout>

关于java - TextView 不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36846440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com