2012-08-22 35 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

我编辑了我的答案。 –