2012-02-12 72 views
0

我花了数小时试图解决这个问题,并花了很长时间来查看其他答案。 我显然错过了简单的事情,但真的一直在尝试。Android布局 - 权重

我把我的代码缩减到非常基础的位置以找到问题。

布局有一个背景,其上半部分是漂亮的图形,低于上半部分我希望有按钮。 背景位于顶部,第一个线性布局(垂直)。 然后,在这里面,有两个垂直线性布局。

顶部垂直布局,具有1 底部垂直布局的重量,具有1

重量这意味着对我来说,顶部布局应占据屏幕的一半。按钮然后进入第二个布局应该可以占用屏幕的其余部分。 但是,当我开始添加到第二个布局时,它不再占用屏幕的一半,而是开始占用上半部分的空间。

我想要做的就是在屏幕的下半部分添加四个图像按钮,并水平集中。

这里是我的代码:

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

    <!-- Layout 1, the top half --> 
    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <!-- Layout 2, the bottom half, containing buttons --> 
    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:src="@drawable/playbtnimg" android:layout_weight="1"/> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:src="@drawable/achievementsbtnimg" android:layout_weight="1"/> 
    </LinearLayout> 

</LinearLayout> 

这两个按钮,现在也开始占用的垂直空间的约2/3。他们不应该进入屏幕的上半部分。 我试图以这种方式使用布局,所以它会在不同的屏幕尺寸上工作。 我希望图像能够缩放以适应其线性布局单元格,而不是在两侧的顶部切断。

任何帮助,非常感谢。

回答

0

一个weightsum属性的根元素添加即父LinearLayout

这里是它会是什么样子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@drawable/menubackground" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:weightSum="2" 
android:orientation="vertical"> 

同样会为LinearLayout有按钮

这将进一步帮助你可以阅读更多在Android文档

+0

我很感激这个帮助。我把2的权值放在包含的布局上,并在其中的两个布局上加上1的权重。它仍然是一样的:(。然后我把重量总和为1在包含按钮的底部布局上,按钮上的重量为0.25,但按钮占用了一半以上 – ThePerson 2012-02-12 21:25:44

+0

好吧,已经走得更基本了,并从Android中拉出hello线性布局文件: http://developer.android.com/resources/tutorials/views/hello-linearlayout。html 我发现的是,当我在顶部LinearLayout上放置背景时,第一行颜色占用不到50%。 这怎么可能与背景有关?我不明白。 – ThePerson 2012-02-12 21:41:32

+0

我看到的是......如果我将背景设置为一种颜色,那很好。只要我把背景图片放在......然后突然就不正确了。有任何想法吗?我甚至确保将我的照片缩放到由Android – ThePerson 2012-02-12 21:45:55

0

尝试添加此c在您使用ImageButtons作为背景的地方。

android:adjustViewBounds="true" 

修复了我遇到的类似问题。

希望它的作品和快乐的编码。

+1

我找到了答案。 我有两个问题。 1)我没有使用比例来缩放图像按钮。 2)我在我的设备上试了一下,然后是一个朋友设备,它在日食界面中显示的设置,发现它在设备上与日食中的不同。看起来它不是100%准确的。 感谢您的所有建议,他们帮助指导了我。 – ThePerson 2012-02-12 23:48:49