Search code examples
androidandroid-linearlayoutandroid-layout-weight

Android Layout Weight Exactly Twice Of Others


I would like to simply divide a Linear Layout to 3 piece. I did something as in the code below. If I change the weight of second inner layout anything else than 2, it works. But if I change it to 2(two), the second inner layout doesn't appear in the design ?

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


Solution

  • Use it like below height set to 0dp when orientation is vertical.

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