前言
本篇博客主要講解ScrollView和HorizontalScrollView兩個容器的使用。它們分別代表了垂直滾動以及水平滾動,滾動的內容是它其中包含的View。在本篇會簡單介紹ScrollView和HorizontalScrollView的使用以及注意事項,最后以一個簡單的Demo來演示一下這兩個容器的使用。
ScrollView
ScrollView,通過官方文檔的繼承關系可以看出,它繼承自FrameLayout,所以它是一種特殊類型的FrameLayout,因為它可以使用用戶滾動顯示一個占據的空間大於物理顯示的視圖列表。值得注意的是,ScrollView只能包含一個子視圖或視圖組,在實際項目中,通常包含的是一個垂直的LinearLayout。
值得注意的是,ScrollView不能和ListView一起使用,因為ListView已經對垂直方向的滾動做了處理,它會迫使如果ListView的內容大於物理視圖的內容的時候,強制垂直滾動的效果,所以這里使用ScrollView和ListView混合使用是沒有意義的,對於ListView的講解,可以參見我的另外一篇博客:Android--UI之ListView。ScrollView還需要注意EditText自帶的多行輸入的滾動效果,也是不可以混合使用的,如果在ScrollView中包含了多行的EditText,那EditText中自帶的滾動效果將失效。其中心思想就是ScrollView是一個滾動視圖的容器,對於一些自帶了滾動效果的控件,是無法和它一起被混合使用的。
在Android平台下,與ScrollView類似的還有一個HorizontalScrollView容器,這個容器與ScrollView的作用相反,主要適用於水平滾動,了解了ScrollView就基本上了解了HorizontalScrollView,所以這里着重講解ScrollView的使用。
示例Demo
ScrollView其實就是一個布局,所以基本上沒有什么太多的自己的方法或者屬性需要特別講解。這里直接展示一個Demo來講解一下使用以及效果即可,這里提供了十張圖片,需要放置在res/drawable-hdpi目錄下。
布局代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="vertical" > 10 11 <TextView 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:text="垂直滾動視圖" 15 android:textSize="30dp" /> 16 17 <ImageView 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:src="@drawable/bmp1" /> 21 22 <ImageView 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:src="@drawable/bmp2" /> 26 27 <ImageView 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:src="@drawable/bmp3" /> 31 32 <EditText 33 android:maxLines="2" 34 android:layout_width="match_parent" 35 android:layout_height="40dp" /> 36 37 <ImageView 38 android:layout_width="match_parent" 39 android:layout_height="wrap_content" 40 android:src="@drawable/bmp4" /> 41 42 <ImageView 43 android:layout_width="match_parent" 44 android:layout_height="wrap_content" 45 android:src="@drawable/bmp5" /> 46 47 <ImageView 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 android:src="@drawable/bmp6" /> 51 52 <ImageView 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:src="@drawable/bmp7" /> 56 57 <ImageView 58 android:layout_width="match_parent" 59 android:layout_height="wrap_content" 60 android:src="@drawable/bmp8" /> 61 62 <ImageView 63 android:layout_width="match_parent" 64 android:layout_height="wrap_content" 65 android:src="@drawable/bmp9" /> 66 67 <ImageView 68 android:layout_width="match_parent" 69 android:layout_height="wrap_content" 70 android:src="@drawable/bmp10" /> 71 </LinearLayout> 72 73 </ScrollView>
效果展示:


HorizontalScrollView
對於HorizontalScrollView而言,其實所有的思想都與ScrollView類似,唯一的區別是HorizontalScrollView是支持水平滾動的。在上面的實例中,只需要改變一下外圍的ScrollView為HorizontalScrollView,再把其中包裹的LinearLayout的android:orientation屬性設置為horizontal即可實現水平滾動的效果。因為沒有什么新的技術含量,這里就不再展示Demo代碼了。
效果展示:


總結
對於現在的Android開發,大部分應用中,需要用到滾動效果的時候,比如說滑動的展示新聞的效果,都會直接使用ListView來裝載數據。但是ScrollView還是有一定用處的,比如一些軟件的屬性的設置,就可以放在一個ScrollView中。核心思想就是對於一些動態的效果展示,就使用ListView,對於固定的一些效果展示,就使用ScrollView包裹即可。
請支持原創,尊重原創,轉載請注明出處。謝謝。

