2013-07-21 38 views
0

我一直在研究一個簡單的聲卡應用程序。基本設計是標題的imageview,後面是4排按鈕(每行3個),這使得它看起來類似於舊手機的按鈕。底部有一個「隨機」和「停止」按鈕。它們佔用的空間比上面提到的行少一些,但隨機按鈕的寬度是空間的兩倍。使用RelativeLayout代替嵌套權重

爲了達到這樣的效果,我一直在使用嵌套權重和線性佈局。但是,經過進一步的研究,我發現這對性能不利,因爲按我目前的格式,每個項目都要測量8次。我的XML佈局代碼如下。我已經排除了很多UI修飾符,例如按鈕上的邊距,文本和陰影,以使代碼更小。代碼可能看起來很長,但它重複且易於理解。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="23" > 

<!-- Insert Title/Picture in Linear Layout below --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="9" 
    android:background="@drawable/etho_logo" 
    android:orientation="horizontal" > 
</LinearLayout> 

<!-- Insert Sounds/Buttons in Linear Layout below --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="5" 
    android:orientation="vertical" 
    android:weightSum="4" > 

    <!-- This is the 1st line of buttons --> 

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

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 
    </LinearLayout> 

    <!-- This is the 2nd line of buttons --> 

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

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 
    </LinearLayout> 

    <!-- This is the 3rd line of buttons --> 

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

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 
    </LinearLayout> 

    <!-- This is the 4th line of buttons --> 

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

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 
    </LinearLayout> 
</LinearLayout> 

<!-- Random Button and Stop button below here --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="9" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

    <Button 
     android:id="@+id/bIntro" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/hello_real_select" /> 

    <Button 
     android:id="@+id/bIntro" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/hello_real_select" /> 
</LinearLayout> 

</LinearLayout> 

我是Android的新手,所以即使我嘗試過使用相對佈局,但我的工作非常困難。我嘗試過在線觀看教程,但在我的代碼中使用它時,它仍然看起來不對。例如,我的標題圖像在我想要顯示的實際文本上方和下方都有額外的空間。在帶有權重的線性佈局中,它會自動變小以適應我給它的空間。雖然不在RelativeLayout中。

我想我在這裏要問的是:我如何使它成爲我的RelativeLayout項目在幾行上均勻隔開的按鈕(如上面的示例所示),以及如何使它成爲ImageViews和其他東西實際上會自動縮小到我給他們的空間?如果需要更多信息,請不要猶豫,問。謝謝。

回答

1

你說得對,RelativeLayout很難製作成表格,比如你上面製作的表格。 LinearLayout對此非常理想,這就是爲什麼TableLayoutTableRow視圖組是LinearLayout的子類。然而,除了RelativeLayout之外,我可以推薦幾種替代方案,這樣可以提高佈局效率。

首先,可以消除第二垂直取向LinearLayout和從1改變第一4行按鈕的佈局重量至1.25。這會將佈局過程的數量從8個減少到4個,這對於一個表格來說是最好的選擇。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="23" > 

    <!-- Insert Title/Picture in Linear Layout below --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="9" 
     android:background="@drawable/etho_logo" 
     android:orientation="horizontal" > 
    </LinearLayout> 

    <!-- This is the 1st line of buttons --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.25" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <!-- 3 buttons here --> 

    </LinearLayout> 

    <!-- This is the 2nd line of buttons --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.25" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <!-- 3 buttons here --> 

    </LinearLayout> 

    <!-- This is the 3rd line of buttons --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.25" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <!-- 3 buttons here --> 

    </LinearLayout> 

    <!-- This is the 4th line of buttons --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.25" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <!-- 3 buttons here --> 

    </LinearLayout> 

    <!-- Random Button and Stop button below here --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="9" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 

     <Button 
      android:id="@+id/bIntro" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/hello_real_select" /> 
    </LinearLayout> 

</LinearLayout> 

另一種選擇是使用單一的佈局,使一個「平」網格(一ViewGroup通過自身管理其子的佈局到表)。如果您的應用的API級別爲14+,則可以查看GridLayout。如果您的目標是14歲以前的API級別,您可能必須自己開發。關於最後一點,請不要混淆GridLayoutGridView視圖組,其目的是要與名單一起使用,並且不會完成你想要的。

+0

非常感謝您的詳細答覆,實際上包括的例子來說明如何修復我的代碼。這對我非常有幫助。我實際上已經決定不公佈的問題後不久使用RelativeLayout的,因此與優化技術提供原代碼你的反應是完美的。 – faraaz

0

建議使用0dp寬度與重量,這應該由測量一半數量的減少(因爲WRAP_CONTENT需要測量,但0dp沒有)

而且,你不需要疊瓦狀垂直線性佈局,因爲它已經是一個線性佈局(雖然它是可讀性更清潔,你可以將其刪除了優化和調整權重)