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