我花了数小时试图解决这个问题,并花了很长时间来查看其他答案。 我显然错过了简单的事情,但真的一直在尝试。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。他们不应该进入屏幕的上半部分。 我试图以这种方式使用布局,所以它会在不同的屏幕尺寸上工作。 我希望图像能够缩放以适应其线性布局单元格,而不是在两侧的顶部切断。
任何帮助,非常感谢。
我很感激这个帮助。我把2的权值放在包含的布局上,并在其中的两个布局上加上1的权重。它仍然是一样的:(。然后我把重量总和为1在包含按钮的底部布局上,按钮上的重量为0.25,但按钮占用了一半以上 – ThePerson 2012-02-12 21:25:44
好吧,已经走得更基本了,并从Android中拉出hello线性布局文件: http://developer.android.com/resources/tutorials/views/hello-linearlayout。html 我发现的是,当我在顶部LinearLayout上放置背景时,第一行颜色占用不到50%。 这怎么可能与背景有关?我不明白。 – ThePerson 2012-02-12 21:41:32
我看到的是......如果我将背景设置为一种颜色,那很好。只要我把背景图片放在......然后突然就不正确了。有任何想法吗?我甚至确保将我的照片缩放到由Android – ThePerson 2012-02-12 21:45:55