2015-04-21 115 views
2

我想围绕android布局来包装头部,并且我认为我会从一个简单的例子开始,但由于某种原因它不能正常工作。基于百分比的android布局

我开始时只是将布局分成4块,并使用背景颜色来证明它是按预期工作的。

此代码为伟大工程:

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_green_light"> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/white"> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_orange_light"> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_red_light"> 
    </RelativeLayout> 

</LinearLayout> 

所以我想继续在部分的故障,并在第二届一个,每个填充50%(有效覆盖它)添加两个新的布局。

我已经尝试了LinearLayout和RelativeLayout,我无法获得任何工作。

这里是我现在所拥有的代码:

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_green_light"> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.5" 
      android:background="@android:color/holo_green_dark" 
      android:layout_alignParentLeft="true" > 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.5" 
      android:background="@android:color/holo_blue_dark" 
      android:layout_alignParentRight="true" > 
     </LinearLayout> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_orange_light"> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_red_light"> 
    </RelativeLayout>Layout> 

</LinearLayout> 

我认为这个问题是在子LinearLayouts的match_parent,但问题是,如果我用WRAP_CONTENT,没有内容,使他们消失。

+1

您应该使用'android:weightSum'属性来实现父级布局。 –

+2

不鼓励嵌套的重量布局。 –

+0

在相对布局中输入一些高度,并添加当前布局的一些屏幕截图。尽量不要使用嵌套布局。 – Codelord

回答

5

请检查更新的xml。它会正常工作

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1" 
    android:layout_height="match_parent"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_green_light"> 
    </RelativeLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:weightSum="1" 
     android:orientation="horizontal" > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.5" 
      android:background="@android:color/holo_green_dark" 
      android:layout_alignParentLeft="true" > 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.5" 
      android:background="@android:color/holo_blue_dark" 
      android:layout_alignParentRight="true" > 
     </LinearLayout> 
    </LinearLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_orange_light"> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.25" 
     android:background="@android:color/holo_red_light"> 
    </RelativeLayout> 
</LinearLayout> 

enter image description here

我已经加入weightsum标签和改变你的相对布局的一个线性布局

+0

Ahhhh,权重现在有意义,我认为它自动为Layouts在同一级别上做。非常感谢! – Tennesseej

+0

@Tennesseej如果你认为我的回答是可以接受的和有帮助的,你可以将其标记为接受:) – deniz

3

RelativeLayout的意见,不会有任何的重量属性的效果。您应该尝试用LinearLayout替换RelativeLayout

+0

这是一个很好的观点和确切的结果! –