Search code examples
androidlayoutandroid-linearlayoutandroid-layout-weight

Equally distribute Nested layouts using weights


I am trying to equally distribute 2 RelativeLayout which is child of Linear Layout.
But when ever make Weight = "1" and width = "0". It just shrinks up into a line.
What I want: Equally Distributed in horizontal manner
problem : It Shrinks into a line

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"                     <!-- here is the problem -->
            android:layout_below="@id/commentry_text_view"
            android:id="@+id/score_and_life"
            android:paddingTop="16dp"
            android:orientation="horizontal">
        <RelativeLayout
            android:layout_weight="1dp"               <!-- Here Is weight -->
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/score"
            style="@style/Header_Text"
            android:id="@+id/score_text_view"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/socreDigit"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"
            style="@style/secondary_text"
            android:id="@+id/Score_Digit_Text_View"
            android:layout_toRightOf="@id/score_text_view"/>
        </RelativeLayout>

            <RelativeLayout
                android:layout_weight="1dp"                    <!-- Here Is weight -->
                 android:layout_marginLeft="150dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/life"
                style="@style/Header_Text"
                android:id="@+id/life_text_view"
                />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/lifeDigit"
                    android:layout_toRightOf="@id/life_text_view"
                    android:layout_marginTop="3dp"
                    android:layout_marginLeft="3dp"
                    style="@style/secondary_text"
                    android:id="@+id/Life_count_Text_view"/>
            </RelativeLayout>
        </LinearLayout>

Solution

  • Set them both to layout_weight="1", not 1dp. The weight value is a fraction, 1dp is a measurement in pixels, so it's getting confused and counting it as zero

    By the way you can remove weightSum="2" (that just means your weights for the full width add up to 2) and just set each weight to 0.5 i.e. 50% of the width. Whichever makes the most sense to you!