布局就是一个 ScrollView 包裹一个超长文章的 TextView ,超过屏幕显示了。
按网上所说, getHeight() 最大就是获取屏幕的高度,但是这里 getHeight() 却和 getMeasuredHeight() 的值相等,都超过我的屏幕高度 1280 了,和网上所说的不符啊。
代码也是最基础的,难道又 Bug 了,或者 getHeight() 和 getMeasuredHeight() 本来就相等?
Log 语句:
scrollView.getMeasuredHeight() + ":" + scrollView.getHeight() + ":" + scrollView.getChildAt(0).getMeasuredHeight() + ":" + scrollView.getChildAt(0).getHeight() + ":" + scrollView.getScrollY() Log:
08-23 14:42:34.428 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:1503 08-23 14:42:34.428 3282-3282/com.example.system.myapplication I/Tag: 底部 08-23 14:42:34.870 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:1248 08-23 14:42:35.355 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:916 08-23 14:42:35.909 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:464 08-23 14:42:36.458 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:0 08-23 14:42:36.458 3282-3282/com.example.system.myapplication I/Tag: 顶部 08-23 14:42:37.998 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:0 08-23 14:42:37.998 3282-3282/com.example.system.myapplication I/Tag: 顶部 08-23 14:42:38.927 3282-3282/com.example.system.myapplication I/Tag: 1230:1230:2733:2733:484 MainActivity.java:
package com.example.system.myapplication; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ScrollView; import android.widget.TextView; public class MainActivity extends Activity implements View.OnTouchListener { private ScrollView scrollView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); TextView textView = (TextView) findViewById(R.id.textView); textView.setText(getResources().getString(R.string.content)); scrollView = (ScrollView) findViewById(R.id.scrollView); scrollView.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.i("Tag", scrollView.getMeasuredHeight() + ":" + scrollView.getHeight() + ":" + scrollView.getChildAt(0).getMeasuredHeight() + ":" + scrollView.getChildAt(0).getHeight() + ":" + scrollView.getScrollY()); if (scrollView.getScrollY() == 0) { Log.i("Tag", "顶部"); } if (scrollView.getScrollY() + scrollView.getHeight() == scrollView.getChildAt(0).getHeight()) { Log.i("Tag", "底部"); } break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return false; } } main_activity.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scrollView"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Text" android:textSize="20sp" android:id="@+id/textView"/> </ScrollView> </LinearLayout> 