2014-03-27 110 views
0

我試圖按照以下方式設計我的佈局。我目前的做法是讓LinearLayout包裝另外兩個LinearLayout。其中每個都有layout_weight=1。然後底部佈局包裝另外兩個,每個也有layout_weight=1。我聽說不建議嵌套的重量 - 但是嗎?否則,什麼是更好的選擇?將視圖拆分成相等部分

enter image description here

感謝

+0

就你而言,嵌套並不是什麼大不了的事情。你有很少的佈局,它不應該以任何方式影響表演。 –

+0

「bottom」兩個視圖的'layout_width'應該是否爲'0dp'? – Kar

+0

嘗試'GridLayout',還有一個老版本的Android版本兼容性問題 – sherpya

回答

0

這將是我的做法:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="2"> 

    <LinearLayout android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1"> 

     <View android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#ff00ff"/> 

    </LinearLayout> 

    <LinearLayout android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:weightSum="2" 
        android:orientation="horizontal"> 

     <View android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="match_parent" 
       android:background="#ffff00"/> 

     <View android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="match_parent" 
       android:background="#00ffff"/> 

    </LinearLayout> 

</LinearLayout> 

相信的權重可能是「低效」,因爲爲了恰當地使用它們,佈局必須首先測量。如果其子女也施加重量,可能會導致重複測量相同的佈局,潛在妨礙表現。

如果您的佈局複雜,您可能需要考慮其他方法(例如,創建一個自定義ViewGroup),但要記住:

我們應該忘記小的效率,說約97%的時間: 過早的優化是所有罪惡的根源。

因此,請確保在嘗試解決問題之前確實遇到問題。