1. 程式人生 > >關於HorizontalScrollView巢狀GridView

關於HorizontalScrollView巢狀GridView

做橫向滑動的時候遇到了幾個問題:

        首先是用HorizontalScrollView直接巢狀GridView,然後發現無論在程式碼是如何設定setNumColumns的值,GridView每行都只顯示一個數據,而且特別窄,圖片顯示不出來,查詢後發現是因為HorizontalScrollView和ScrollView一樣,下面需要一層根佈局,在HorizontalScrollView和GridView之間加了一層LinearLayout然後給HorizontalScrollView新增一條屬性android:fillViewport="true"     當ScrollView裡的元素想填滿ScrollView時,使用"fill_parent"是不管用的,必需為ScrollView設定:android:fillViewport="true"。

            之後又發現佈局最多隻能充滿螢幕,資料越多,每個item就越小,會讓所有的item同時擠在螢幕裡,而不能橫向滑動,這是因為gridview寬度不能寫wrap_content或者match_parent,而需要寫固定值,我們可以設定Gridview的layout_width=2000dp,一個可以超出螢幕最大寬度的值。

<HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <GridView android:id="@+id/activity_userinfo_gv" android:layout_width="2000dp" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/px40_dp"></GridView> </LinearLayout> </HorizontalScrollView> </LinearLayout>