前言html
本篇博客主要講解ScrollView和HorizontalScrollView兩個容器的使用。它們分別表明了垂直滾動以及水平滾動,滾動的內容是它其中包含的View。在本篇會簡單介紹ScrollView和HorizontalScrollView的使用以及注意事項,最後以一個簡單的Demo來演示一下這兩個容器的使用。android
ScrollView佈局
ScrollView,經過官方文檔的繼承關係能夠看出,它繼承自FrameLayout,因此它是一種特殊類型的FrameLayout,由於它可使用用戶滾動顯示一個佔據的空間大於物理顯示的視圖列表。值得注意的是,ScrollView只能包含一個子視圖或視圖組,在實際項目中,一般包含的是一個垂直的LinearLayout。.net
值得注意的是,ScrollView不能和ListView一塊兒使用,由於ListView已經對垂直方向的滾動作了處理,它會迫使若是ListView的內容大於物理視圖的內容的時候,強制垂直滾動的效果,因此這裏使用ScrollView和ListView混合使用是沒有意義的,對於ListView的講解,能夠參見個人另一篇博客:Android--UI之ListView。ScrollView還須要注意EditText自帶的多行輸入的滾動效果,也是不能夠混合使用的,若是在ScrollView中包含了多行的EditText,那EditText中自帶的滾動效果將失效。其中心思想就是ScrollView是一個滾動視圖的容器,對於一些自帶了滾動效果的控件,是沒法和它一塊兒被混合使用的。xml
在Android平臺下,與ScrollView相似的還有一個HorizontalScrollView容器,這個容器與ScrollView的做用相反,主要適用於水平滾動,瞭解了ScrollView就基本上了解了HorizontalScrollView,因此這裏着重講解ScrollView的使用。htm
示例Demoblog
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>
效果展現:utf-8
HorizontalScrollView
對於HorizontalScrollView而言,其實全部的思想都與ScrollView相似,惟一的區別是HorizontalScrollView是支持水平滾動的。在上面的實例中,只須要改變一下外圍的ScrollView爲HorizontalScrollView,再把其中包裹的LinearLayout的android:orientation屬性設置爲horizontal便可實現水平滾動的效果。由於沒有什麼新的技術含量,這裏就再也不展現Demo代碼了。
效果展現:
總結
對於如今的Android開發,大部分應用中,須要用到滾動效果的時候,好比說滑動的展現新聞的效果,都會直接使用ListView來裝載數據。可是ScrollView仍是有必定用處的,好比一些軟件的屬性的設置,就能夠放在一個ScrollView中。核心思想就是對於一些動態的效果展現,就使用ListView,對於固定的一些效果展現,就使用ScrollView包裹便可。
請支持原創,尊重原創,轉載請註明出處。謝謝。