GridView嵌套佈局

有時候開發,需要滑動佈局的嵌套,而當我們將佈局嵌套好了以後,問題就出來,高度不精確,這時候是否可以想想自己哪裏出錯了呢,大家先看一下有問題的佈局  <LinearLayout
            android:id="@+id/child_root"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="7"
            android:background="@color/LIGHT_GRAY"
            android:orientation="vertical"
            android:paddingLeft="5dp" >


            <LinearLayout
                android:id="@+id/LinearLayout_child"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center_vertical" >


                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:scrollbars="none" >


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center_vertical"
                        android:orientation="horizontal" >


                        <GridView
                            android:id="@+id/lv_category_two"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:gravity="center_vertical"
                            android:scrollbars="none" >
                        </GridView>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>


            <LinearLayout
                android:layout_below="@+id/LinearLayout_child"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/LIGHT_GRAY"
                android:baselineAligned="true" >


                <GridView
                    android:id="@+id/gv_common_product"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:horizontalSpacing="5dp"
                    android:scrollbars="none"
                    android:verticalSpacing="5dp" >
                </GridView>
            </LinearLayout>

       </LinearLayout>

以上佈局是顯示頭一個橫向的滑動列表,下面顯示數據的GridView,看上去一點問題都沒有,但是當我們運行出來就會發現,這樣的佈局下面的GridView高度是自適應的,即內容有多少就顯示多高,這樣會造成數據量小的時候屏幕下方出現大量的留白,而且有時候會出現數據高度計算出錯的問題,這樣的佈局經常是以上一次獲得的數據的高度作爲下一次數據顯示的高度,這就是問題所在了,當第一次數據只佔一行第二次數據佔多行時,這時的gridView會在一行裏面上下滑動,鄙人也是初次見到,查了很多資料未果,後經多次修改得出方法,其實很簡單,但是鄙人至今未能理解其爲何如此,若有大神知道其所以然,還望不吝賜教,小弟感激不盡。在此奉上修改方法。方法就是將最外層的LinearLayout(即紅色部分)修改爲RelativeLayout。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章