標籤:樣本 dem androi size ext dev develop reference htm
前言
本篇部落客要講解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 <TextView12 android:layout_width="match_parent"13 android:layout_height="wrap_content"14 android:text="垂直滾動視圖"15 android:textSize="30dp" />16 17 <ImageView18 android:layout_width="match_parent"19 android:layout_height="wrap_content"20 android:src="@drawable/bmp1" />21 22 <ImageView23 android:layout_width="match_parent"24 android:layout_height="wrap_content"25 android:src="@drawable/bmp2" />26 27 <ImageView28 android:layout_width="match_parent"29 android:layout_height="wrap_content"30 android:src="@drawable/bmp3" />31 32 <EditText33 android:maxLines="2"34 android:layout_width="match_parent"35 android:layout_height="40dp" />36 37 <ImageView38 android:layout_width="match_parent"39 android:layout_height="wrap_content"40 android:src="@drawable/bmp4" />41 42 <ImageView43 android:layout_width="match_parent"44 android:layout_height="wrap_content"45 android:src="@drawable/bmp5" />46 47 <ImageView48 android:layout_width="match_parent"49 android:layout_height="wrap_content"50 android:src="@drawable/bmp6" />51 52 <ImageView53 android:layout_width="match_parent"54 android:layout_height="wrap_content"55 android:src="@drawable/bmp7" />56 57 <ImageView58 android:layout_width="match_parent"59 android:layout_height="wrap_content"60 android:src="@drawable/bmp8" />61 62 <ImageView63 android:layout_width="match_parent"64 android:layout_height="wrap_content"65 android:src="@drawable/bmp9" />66 67 <ImageView68 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包裹即可。
請支援原創,尊重原創,轉載請註明出處。謝謝。
Android--UI之ScrollView