Android 布局权重恰好是其他人的两倍

Android Layout Weight Exactly Twice Of Others

我想简单地将一个线性布局分成 3 块。我按照下面的代码做了一些事情。如果我将第二个内部布局的权重更改为 2 之外的任何其他内容,它就会起作用。但是,如果我将其更改为 2(two),则第二个内部布局不会出现在设计中?

<LinearLayout
android:id="@+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    >
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:gravity="center"
    >
    <ImageView
        android:id="@+id/HomePageIcon"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingHorizontal="33dp"
    android:paddingBottom="30dp"
    >
</LinearLayout>

当方向为垂直时,将其设置为 0dp 以下高度。

<LinearLayout
    android:id="@+id/Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    android:orientation="vertical"
    android:gravity="center"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:layout_weight="1"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@color/background_dark"
        android:gravity="center"
        >
        <ImageView
            android:id="@+id/HomePageIcon"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:adjustViewBounds="true" />
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical"
        android:paddingHorizontal="33dp"
        android:paddingBottom="30dp"
        >
    </LinearLayout>

使用约束布局,忘记线性和相对布局。

请试试这个,

<LinearLayout
    android:id="@+id/Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    android:orientation="vertical"
    android:gravity="center"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        >
        <ImageView
            android:id="@+id/HomePageIcon"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:adjustViewBounds="true" />
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:paddingHorizontal="33dp"
        android:paddingBottom="30dp"
        >
    </LinearLayout>

当您使用 layout_weight 时,这意味着您的内部布局将采用尽可能多的高度。因此,在这种情况下,无需使用 layout_height = "wrap_content" 只需将其更改为 android:layout_height="0dp"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:weightSum="4">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"></LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:gravity="center">

    <ImageView
        android:id="@+id/HomePageIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:paddingBottom="30dp"
    android:paddingHorizontal="33dp"></LinearLayout>

</LinearLayout>

当您使用权重时,请使用 0dpandroid:layout_widthandroid:layout_height 以影响使用权重。在父布局

中使用android:weightSum很好
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical"
    android:weightSum="4">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center">

        <ImageView
            android:id="@+id/HomePageIcon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingBottom="30dp"
        android:paddingHorizontal="33dp"></LinearLayout>