首页 > 解决方案 > Android - layout_weight - LinearLayout 中的大小问题

问题描述

我需要创建一个具有一定纵横比并且具有响应性的布局。

块将被放置在另一个之上。我需要在以下命题中定义组件: Block 1:10% Block 2:5% Block 3:60% Block 4:15% Block 5:10%

这是我的模板的样子:

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

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_red_dark"
        android:orientation="horizontal"></LinearLayout>

    <LinearLayout
        android:id="@+id/l2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_dark"
        android:orientation="horizontal"></LinearLayout>

    <LinearLayout
        android:id="@+id/l3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_green_dark"
        android:orientation="horizontal"></LinearLayout>

    <LinearLayout
        android:id="@+id/l4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_purple"
        android:orientation="horizontal"></LinearLayout>

    <LinearLayout
        android:id="@+id/l5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_orange_dark"
        android:orientation="horizontal"></LinearLayout>
</LinearLayout>

我尝试通过 layout_weight 确定: 块 1:1 或 10 块 2:0.5 或 5 块 3:6 或 60 块 4:1.5 或 15 块 5:1 或 10

但一切都崩溃了。

它是如何工作的?我究竟做错了什么?

标签: android

解决方案


android:layout_height="0dp"如果父项的方向是垂直的,则必须设置为所有子项


推荐阅读