繁体   English   中英

基于百分比的Android布局

[英]Percentage based 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>

因此,我希望继续细分这些部分,并在第二部分上添加两个新布局,每个布局填充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,则没有内容因此它们会消失。

请检查更新的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>

在此输入图像描述

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

RelativeLayout的视图不会影响权重属性。 您应该尝试使用LinearLayout替换RelativeLayout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM