ScrollView使用筆記

  1. ScrollView的實際大小超過手機屏幕的顯示範圍(在Y軸上);如下圖所示,手機屏幕相當於一個滑動窗沿Y軸方向在整個ScrollView中滑動;
  2. ScrollView只能含有一個子View(當然這View可以是一個Group,如Layout,即可含有多個View);所以,可以這樣理解,這個包含的子View的顯示範圍即ScrollView的全部顯示範圍;
佈局XML示例:
說明:
由此可見,自定義ScrollView(com.example.photowallfallsdemo.MyScrollView)下,僅僅含有一個子View(LinearLayout)。其中,這個子View(LinearLayout)可以含有三個子View(LinearLayout)
如下所示:
<com.example.photowallfallsdemo.MyScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/first_column"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/second_column"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/third_column"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

</com.example.photowallfallsdemo.MyScrollView>

      3.各主要參數及函數使用:
注意比較b與c兩點
a、獲取手機屏幕顯示範圍相對於整個ScrollView的Y軸偏移量:
myScrollView.getScrollY();
b、獲取整個ScrollView的高度
(在2點中已經說過,ScrollView只能含有一個子View,且這個子View的大小即ScrollView的大小
scrollLayout = getChildAt(0);//獲取MyScrollView的唯一一個子View
scrollLayout.getHeight();
c、獲取手機顯示範圍的高度:
getHeight();//相對於MyScrollView

示意圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章