0

I have a simple LinearLayout with an ImageView and a normal view having layout weights 0.55 and 0.45 respectively.

I wasn't able to split unless I gave a weightsum to my LinearLayout.

After having my LinearLayout split, I tried to give a background color to my view but its not reflecting.

Below is my code:

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" > <ImageView android:layout_weight=".55" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="5dip" android:scaleType="fitXY" android:src="@drawable/ic_launcher_background" android:contentDescription="TODO" /> <View android:layout_weight=".45dp" android:layout_width="match_parent" android:layout_height="0dp" android:visibility="visible" android:background="@color/colorPrimary" ></View> </LinearLayout> 

I can't understand what shall be changed such that, I can split the views correctly and set the background color. Thanks in advance!

1 Answer 1

3

Replace "android:layout_weight=".45dp" to "android:layout_weight=".45"

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1"> <ImageView android:layout_weight=".55" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="5dip" android:scaleType="fitXY" android:src="@drawable/ic_launcher_background" android:contentDescription="TODO" /> <View android:layout_weight=".45" android:layout_width="match_parent" android:layout_height="0dp" android:visibility="visible" android:background="@color/colorPrimary"/> </LinearLayout> 
Sign up to request clarification or add additional context in comments.

6 Comments

Such an error!! But thanks dude!! is weightsum option mandatory??
Yes we can use the weight without defining the weight sum in parent. IN that case the total weight of the child is calculated by adding the weight of each child.
if you get your answer so approve it... Thank you
cool !, if u don't mind, I have an alignment issue which I have raised separately.could u have a look at it:stackoverflow.com/questions/56174608/…
@adi weightsum specify the Parts of weight regardless of view is Visible or Not . If you specify it and make any Child GONE that space will be blank . If you do not specify it then it will divide according to visible Views.. Cappese ???
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.