Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android View.getTop()始终返回0_Android - Fatal编程技术网

Android View.getTop()始终返回0

Android View.getTop()始终返回0,android,Android,因此,View.getTop()是,根据: 此视图相对于其父视图的顶部位置 我的布局如下: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/root"

因此,
View.getTop()
是,根据:

此视图相对于其父视图的顶部位置

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.scrollviewtest1.MainActivity">

    <LinearLayout
        android:id="@+id/scroll_container"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.example.scrollviewtest1.MyTextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/large_text" />

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

    </LinearLayout>


</ScrollView>
并在子对象的调用
TextView

onDraw()中进行检查,请尝试以下方法:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            int top = view.getTop();
        }

    }, 300);// delay times in milliseconds

您可以根据需要设置延迟时间。

获取LinearLayout滚动位置调用
myScrollView.getScrollY()


getTop()
返回您在xml布局中作为marginTop输入的内容,滚动时它不会改变,以了解您需要考虑的实际当前位置
getScrollY()太晚了

延迟几秒钟,然后检查。@Jaymin:你能详细说明一下吗?怎么查?怎么查?有什么可以解释的吗你能把你的密码贴出来吗?所以我可以检查它并向你解释。@Zoe:我已经添加了我如何检查它的部分。只是试了一下。当我上下滚动时,它显示0。我已将边距添加到
线性布局中
,它现在为我提供了作为
getTop()
添加的确切边距量。我看到
ScrollView扩展了Framelayout
。对于
getTop()
,没有给出一个正确的值到底有什么不同?
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            int top = view.getTop();
        }

    }, 300);// delay times in milliseconds