android - 基于百分比的android布局

标签 android android-layout

我正在努力研究 android 布局,我想我会从一个简单的例子开始,但由于某种原因它不起作用。

我首先将布局分成 4 个 block ,并使用背景颜色来证明它按预期工作。

此代码非常适合:

<?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>

enter image description here

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

关于android - 基于百分比的android布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763757/

相关文章:

android - Lua/电晕SDK : Why is my countdown stopping?

android - 在android中增加TextView下划线高度

java - 编辑 xml 元素的 "app"属性

java - Android Studio activity_main.xml 设计编辑器不允许我拖放任何东西......?

android - 启动图像大小

android - 为什么不根据位置设置背景[android]

android - 如何将 float 操作按钮放置在约束布局内屏幕的右下角?

android - 如何自定义对话框的标题布局

java - 如何从 Firebase 中检索 ListView 中的数据

java - 仅使用一台物理设备测试 Android 实时多人游戏