Swapnil Padaya
Swapnil Padaya

Reputation: 695

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>

Upvotes: 0

Views: 67

Answers (2)

Vipin Chouhan
Vipin Chouhan

Reputation: 128

I have resolve solve your issue by changing android:layout_weight="1dp" to android:layout_weight="1" from both relative layout and remove unnecessary margin from both layout. please check below XML fix that resolve your issue.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:id="@+id/score_and_life"
    android:paddingTop="16dp"
    android:orientation="horizontal">
    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="score"

            android:id="@+id/score_text_view"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="socreDigit"
            android:layout_marginTop="3dp"


            android:id="@+id/Score_Digit_Text_View"
            android:layout_toRightOf="@id/score_text_view"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="rfymkt,"

            android:id="@+id/life_text_view"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ejnetjn"
            android:layout_toRightOf="@id/life_text_view"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"

            android:id="@+id/Life_count_Text_view"/>
    </RelativeLayout>
</LinearLayout>

Upvotes: 0

cactustictacs
cactustictacs

Reputation: 19524

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!

Upvotes: 1

Related Questions