2013-01-07 132 views
1

我試圖通過展開視圖層次結構來優化Android應用程序中的佈局。這是一個特別困難的!如何優化此佈局?

A very inefficient layout

這種佈局有一個主的LinearLayout以保持頂部和底部行(這是水平方向的子LinearLayouts本身)。中間的四個項目中的每一個都是垂直的RelativeLayout(以適應ImageView和textView)使用layout_weights分佈。每行包含兩個項目也是一個水平LinearLayout。不用說,這種佈局效率低下,導致很多「編舞者跳過幀」的信息在繪製時出現。我想消除這些嵌套的佈局,但是AFAIK RelativeLayout在平行排列行中的項和垂直排列在中間的兩行中沒有幫助。另外,我考慮用複合drawable替換ImageView和TextView,但我不知道如何控制drawable的大小。

任何幫助表示讚賞!

編輯: 下面是佈局的粗略描述。

<!-- The top bar --> 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="80dip" 
android:background="@drawable/some_image"> 
    ... 
</LinearLayout> 

<!-- Central Layout --> 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:weightSum="2" 
android:orientation="vertical"> 

    <!-- First Row --> 
    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:baselineAligned="false" 
    android:layout_marginTop="10dp" 
    android:layout_weight="1" 
    android:weightSum="2"> 


     <!-- Item 1 --> 
     <RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical"> 

     <!-- ImageView and TextView here --> 

     </RelativeLayout> 

     <!-- Item 2 --> 
     <RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical"> 

     <!-- ImageView and TextView here --> 

     </RelativeLayout> 

    </LinearLayout> 
    <!-- End of first row layout --> 

    <!-- Second Row --> 
    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:baselineAligned="false" 
    android:layout_marginTop="10dp" 
    android:layout_weight="1" 
    android:weightSum="2"> 


     <!-- Item 3 --> 
     <RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical"> 

     <!-- ImageView and TextView here --> 

     </RelativeLayout> 

     <!-- Item 4 --> 
     <RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical"> 

     <!-- ImageView and TextView here --> 

     </RelativeLayout> 

    </LinearLayout> 
<!-- End of second row layout --> 

</LinearLayout> 
<!-- End of central layout --> 

<!-- Bottom bar -->  
<LinearLayout...> 
.... 
</LinearLayout> 

+1

發佈你現有的xml文件不是很有幫助,而不是解釋 – Bigflow

+0

相信我,你不想看到它!不過,我想我可以添加一個「僞佈局」。 – WeNeigh

回答

0

你的榜樣看起來很像由Android團隊在這裏給出的演示所示: http://developer.android.com/reference/android/widget/GridLayout.html

我絕不與網格佈局(大量的試驗和錯誤之前,我得到它的工作),但超級精明的手段看看你有什麼它應該做的竅門(尤其是如果你將所有的按鈕在底部移動到頂部,並把它們作爲一個ActionBar)。

擺脫layout_weights將改善一切很多,因爲每次使用它們時,他們一次又一次地測量。