如果在relativeLayout中滚动视图,则ScrollTo不起作用

时间:2018-12-06 13:03:57

标签: android

我在scrollView中有relativeLayout,我想将视图滚动到某个位置。但是,如果我在相对布局内部有滚动视图,或者滚动视图具有width="match_parent",则无法移动它(我需要rv或width="match_parent"才能在完整布局上设置背景color)。在这种情况下如何将rl或scrollView移动到某个位置?

    <ScrollView
                android:id="@+id/scroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:overScrollMode="never"
                android:scrollbars="none"
                android:background="@color/white">

                <RelativeLayout
                    android:id="@+id/scroll_rl"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
android:id="@+id/scroll_to_this"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingStart="28dp"
                        android:paddingEnd="28dp"
                        android:textColor="@color/white" />

    <!-- Some content-->
                </RelativeLayout>
            </ScrollView>

我的代码

scroll.scrollTo(0, scroll_to_this.bottom)

2 个答案:

答案 0 :(得分:0)

您可以使用getY()获得视图的y位置。使用下面的代码滚动到textview的Y值。

scroll.smoothScrollTo (0, (int) scroll_to_this.getY());

答案 1 :(得分:0)

请在屏幕截图中添加更多代码或描述,以便我们了解您的实际需求。除了有关代码和描述的内容以外,如果您要滚动视图,则只有在滚动视图的子视图变得更多而不适合屏幕高度时,它才起作用,然后可以滚动它。为此,您可以通过以下两种方式使用它:

scrollView.scrollTo(0, tv.getY());

scrollView.smoothScrollTo(0, tv.getY());

在这里您还可以像tv.getBottom()一样使用视图的底部位置。

相关问题