android開發學習筆記系列(3)--ScrollView與HorizontalScrollView

ScrollView與HorizontalScrollView

這是一個滾動視圖,就是說若是你在你的UI中容不下那麼多的內容,且你對本身的UI都已經設置好了px,OK,那麼在適應屏幕過程當中,咱們並不但願使之壓縮,因而咱們能夠採用ScrollView這個空間來包裹!html

注意,值得一提的是,它只能包裹一個組件,所以,你必須在這個過程當中只能使用一個Layout來佈局哦!android

固然上面所說的是,是垂直方向上的佈局,若是是要採用水平佈局的話,那麼你能夠採用HorizontalScrollView!佈局

詳細介紹以上兩個控件

ScrollView

ScrollView,經過官方文檔的繼承關係能夠看出,它繼承自FrameLayout,因此它是一種特殊類型的FrameLayout,由於它可使用用戶滾動顯示一個佔據的空間大於物理顯示的視圖列表。值得注意的是,ScrollView只能包含一個子視圖或視圖組,在實際項目中,一般包含的是一個垂直的LinearLayout。學習

值得注意的是,ScrollView不能和ListView一塊兒使用,由於ListView已經對垂直方向的滾動作了處理,它會迫使若是ListView的內容大於物理視圖的內容的時候,強制垂直滾動的效果,因此這裏使用ScrollView和ListView混合使用是沒有意義的,對於ListView的講解,能夠參見個人另一篇博客:Android--UI之ListView。ScrollView還須要注意EditText自帶的多行輸入的滾動效果,也是不能夠混合使用的,若是在ScrollView中包含了多行的EditText,那EditText中自帶的滾動效果將失效。其中心思想就是ScrollView是一個滾動視圖的容器,對於一些自帶了滾動效果的控件,是沒法和它一塊兒被混合使用的。code

在Android平臺下,與ScrollView相似的還有一個HorizontalScrollView容器,這個容器與ScrollView的做用相反,主要適用於水平滾動,瞭解了ScrollView就基本上了解了HorizontalScrollView,因此這裏着重講解ScrollView的使用。xml

ScrollView其實就是一個佈局,因此基本上沒有什麼太多的本身的方法或者屬性須要特別講解。這裏直接展現一個Demo來說解一下使用以及效果便可,這裏提供了十張圖片,須要放置在res/drawable-hdpi目錄下。htm

佈局文件xml:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="垂直滾動視圖"
        android:textSize="30dp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp1" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp2" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp3" />

    <EditText
        android:maxLines="2"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp4" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp5" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp6" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp7" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp8" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp9" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bmp10" />
</LinearLayout>

效果圖:blog

HorizontalScrollView

對於HorizontalScrollView而言,其實全部的思想都與ScrollView相似,惟一的區別是HorizontalScrollView是支持水平滾動的。在上面的實例中,只須要改變一下外圍的ScrollView爲HorizontalScrollView,再把其中包裹的LinearLayout的android:orientation屬性設置爲horizontal便可實現水平滾動的效果。繼承

以上是借用了園友的博客,由於這個實在是不必詳細地進行介紹,在此很是感謝那位園友的分享,在這裏,特地貼出以上資源的來源-------Android--UI之ScrollView圖片

若是有人非說我無恥拿園友的博客轉載的話,在此表示深深的歉意,我認可轉載了!但我仍然會遵循本身的寫文原則:分享本身想分享的,寫本身想寫的,引用必寫明原處,不辜負任何一位願意出來分享的人,讓你們都能和平地學習與工做!

相關文章
相關標籤/搜索