0

I know the effect of setting layout_width (or height) to 0dp in combination with layout_weight in a LinearLayout, as answered in this question:

Why is 0dp considered a performance enhancement?

However, in the example code on the Android developer guide here

http://developer.android.com/guide/topics/ui/layout/relative.html

Why do they set the layout_width of the Spinner to 0dp ? So what is exactly the reason or the trick behind this ? My guess for this is the horizontal position for the Spinner can already be determined by the relative attributes, so they just specify it as 0dp for the same reason as above case. However, I can't seem to find actual documentation on this so it's still a bit confusing.

Community
  • 1
  • 1
ramcrys
  • 643
  • 4
  • 14

3 Answers3

3

When a child view is anchored on both sides (in RelativeLayout), the width is ignored. In this case, the Spinner knows it width because of

android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times"
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
1

I've seen this trick in several projects too, and the reason is exactly the same why they do it in LinearLayout with "weight", but here the key attribute is: android:layout_toLeftOf="@+id/times" it does exactly the same trick as "weight" would do with linear layouts, so this is a way to say, take all the space remaining from android:layout_alignParentLeft="true" until android:layout_toLeftOf="@+id/times" without hardcoding any specific width...

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
-1

we use 0dp while using with layout_weight because it will neglect that attribute and it will go as per the layout_weight..

ʌɐɥqıɐʌ
  • 137
  • 16