Android ScrollView子元素位置

时间:2012-12-12 11:46:55

标签: android android-layout scrollview

我有一个ScrollView,我想滚动到一个子元素(一个相对视图), 我知道如何滚动到X / Y位置,但如何知道我的相对视图在Scroll View中的位置? 或者也许可以直接滚动到这个相对视图?

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
....
        <RelativeLayout
            android:id="@+id/box"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
....
</ScrollView>

我想滚动到@ + id / box。有可能吗?

1 个答案:

答案 0 :(得分:0)

要滚动到特定元素,我将从之前的所有元素中添加“.getHeight()”并添加topMargin,然后滚动到此位置

获得topMargin:

MarginLayoutParams vlpBox = (MarginLayoutParams) box1.getLayoutParams();
vlpBox.topMargin

获得真正的高度(不是wrap_content):

View.getHeight()

这是一种解决方法,我不知道是否存在更好的解决方案。