基于百分比的Android布局

Ten*_*eej 2 android android-layout

我试图围绕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>
Run Code Online (Sandbox Code Playgroud)

因此,我希望继续细分这些部分,并在第二部分上添加两个新布局,每个布局填充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>
Run Code Online (Sandbox Code Playgroud)

我认为问题在于子LinearLayouts中的match_parent,但问题是如果我使用wrap_content,则没有内容因此它们会消失.

den*_*niz 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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我添加了权重标签并将您的一个相对布局更改为线性布局