2012-08-22 27 views
0

我想配置我的按鈕佈局,並且無法正確形成結構。當我在VM上運行代碼時,儘管重量僅爲10%,ImageButton廣告佔據了屏幕的50%。每個圖像按鈕引用一個存儲在drawable-ldpi中的圖像,每個圖像大小約爲1 KB,活動代碼基本沒有添加,這是純粹的圖形界面,此時通過主要的xml。如何讓線性佈局中的我的體重正確對齊按鈕?

INTENDED佈局:

_______________________ 
|  logo   | 
|______________________| 
|  search   | 
|______________________| 
|      | 
|  featured   | 
|      | 
|______________________| 
| |  |  |  | 
| |  |  |  | 
| |  |  |  | 
| |  |  |  | 
| |  |  |  | 
| |  |  |  | 
| |  |  |  | 
|____|_____|_____|_____| 
|  advertisement | 
|______________________| 

ANDROID的main.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/toplayout" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="45" 
     android:gravity="top" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/mytastelogo" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="15" 
      android:src="@drawable/mytastelogo_image" /> 

     <ImageButton 
      android:id="@+id/search" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="10" /> 

     <ImageButton 
      android:id="@+id/featuredpanel" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="20" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottomlayout" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="45" 
     android:gravity="bottom" 
     android:orientation="horizontal" > 

     <ImageButton 
      android:id="@+id/mycollection" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/mycollection_image" /> 

     <ImageButton 
      android:id="@+id/myrecommendations" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/myrecommendations_image" /> 

     <ImageButton 
      android:id="@+id/topitems" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/topitems_image" /> 

     <ImageButton 
      android:id="@+id/randomitem" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/randomitem_image" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/adlayout" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="10" 
     android:gravity="end" > 

     <ImageButton 
      android:id="@+id/advertisement" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

</LinearLayout> 

回答

1

試試吧:-)人

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainlayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/toplayout" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="45" 
    android:gravity="top" 
    android:orientation="vertical" > 

    <ImageButton 
     android:id="@+id/mytastelogo" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="15" /> 

    <ImageButton 
     android:id="@+id/search" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="10" /> 

    <ImageButton 
     android:id="@+id/featuredpanel" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/bottomlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="45" 
    android:gravity="bottom" 
    android:orientation="horizontal" > 

    <ImageButton 
     android:id="@+id/mycollection" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/myrecommendations" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/topitems" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/randomitem" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/adlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="10" > 

    <ImageButton 
     android:id="@+id/advertisement" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

+0

謝謝!這似乎完成了這項工作。 – jrquick

0

您需要設置layout_height0dp當你的方向vertical。當它是horizontal時,請將您的layout_width設置爲0dp

例如:

<LinearLayout 
     android:id="@+id/toplayout" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="45" 
     android:gravity="top" 
     android:orientation="vertical" > 
+0

這使按鈕完全消失,剩下的其他按鈕仍停留在頂部。 – jrquick

+0

當你的LinearLayout方向是水平的時,你的'layout_width'應該是0dp,當它是垂直的時候,'layout_height'應該是0dp。 – nhaarman

+0

是的,這原本是我在上一個問題中得到了我的代碼,修復這個問題的答案是將0dp更改爲match_parent。現在,當我切換回來時,我又回到了按鈕不出現的問題。 – jrquick

-1

所有項目進行權重,這將決定他們的身高應該有安卓layout_height設置爲0dp。

android:layout_height="0dp" 

同樣爲寬度項目是由重量來決定,你必須設置

android:layout_width="0dp" 
+0

這會導致佈局中的按鈕集完全消失。 – jrquick

+0

它你想高度由重量決定,你使高度等於0dp並分配重量。如果你想通過權重來分配寬度,你可以使用寬度屬性,但將高度保持爲正常值。 –

+0

我編輯了我的答案。 –