gpt4 book ai didi

android - 在水平 ScrollView 中滚动文本

转载 作者:行者123 更新时间:2023-11-29 16:01:48 25 4
gpt4 key购买 nike

我看到了一些与我的问题类似的帖子。但是,这些帖子中提到的解决方案并没有帮助我解决问题。这是场景。我在水平 ScrollView 中有一个 TextView。这个想法是,一旦文本(大于 View )附加到 TextView,它就必须自动滚动到结束。这是布局 fragment 。

<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/subTitleColor"
android:fillViewport="true"
android:scrollbars="none" >

<TextView
android:id="@+id/drillPath"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="this is a such a long text that scrolling is needed, scroll more"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/textColorWhite" />

</HorizontalScrollView>

在发生事件时,我执行以下操作。

textView.append("do auto scroll");

实际上,它应该将文本滚动到末尾。但事实并非如此!我缺少什么?

最佳答案

首先,您的 ScrollView 永远不会滚动,因为它的内容 View (TextView)处于 fill_parent 模式。它应该包装它的内容以便为文本留出空间并且比它的父级大。 (将您的 textView 宽度更改为:android:layout_width="wrap_content")

然后,这里没有请求滚动的东西,我认为它不应该自动发生。

添加这样的东西来请求滚动到 ScrollView 的末尾:

   textView.append("do auto scroll");

scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(View.FOCUS_RIGHT);
}
});

尝试过后请告诉我。

关于android - 在水平 ScrollView 中滚动文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23950081/

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