Scroll text in a horizontal scroll view

末鹿安然 提交于 2020-01-14 03:56:07

问题


I have seen a few posts similar to my issue. However, the solutions mentioned in those posts did not help me fix the problem. Here is the scenario. I have a TextView in a horizontal scroll view. The idea is as soon as a text (larger than the view) is appended to the TextView, it must be automatically scrolled to end. Here is the layout snippet.

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

Upon an event, I do the following.

textView.append("do auto scroll");

Effectively, it should scroll the text to the end. But it isn't! What is it I am lacking?


回答1:


First of all, your scrollview will never scroll since its content view (the TextView) is in fill_parent mode. it should wrap its content in order to leave space for the text and be bigger than its parent. (change your textView width to : android:layout_width="wrap_content" )

Then, there is nothing here to request the scroll, I don't think it should happen automatically.

Add something like this to request the scroll to the end of your scroll view :

   textView.append("do auto scroll");

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

Let me know when you've tried it.




回答2:


Let try to fix your TextView height and change this width layoutParam to wrap_content

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

            <TextView
                android:id="@+id/drillPath"
                android:layout_width="wrap_content"  <-- Here
                android:layout_height="10dp"         <-- Here
                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> 

Because of this layoutParam, Your text do not have enough size to scroll. It only have max width is screen width.

Hope that help.



来源:https://stackoverflow.com/questions/23950081/scroll-text-in-a-horizontal-scroll-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!