Android布局重量恰好是其他两倍

时间:2018-01-31 05:02:20

标签: android android-linearlayout android-layout-weight

我想简单地将线性布局划分为3个。我在下面的代码中做了一些事情。如果我改变第二个内部布局的重量除了2以外,它就有效。但是,如果我将其更改为2(2),第二个内部布局不会出现在设计中?

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

5 个答案:

答案 0 :(得分:1)

当方向垂直时,将其设置为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>

答案 1 :(得分:0)

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

答案 2 :(得分:0)

请试试这个,

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

答案 3 :(得分:0)

当您使用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>

答案 4 :(得分:0)

当您使用体重时,使用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>