[英]Set layout_width (height) to 0dp inside RelativeLayout
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: 我知道将Linear_Layout中的layout_width(或height)设置为0dp并与layout_weight结合使用的效果,如以下问题所示:
Why is 0dp considered a performance enhancement? 为什么0dp被视为性能增强?
However, in the example code on the Android developer guide here 但是,在此处的Android开发人员指南中的示例代码中
http://developer.android.com/guide/topics/ui/layout/relative.html http://developer.android.com/guide/topics/ui/layout/relative.html
Why do they set the layout_width of the Spinner to 0dp ? 他们为什么将微调器的layout_width设置为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.
我的猜测是Spinner的水平位置已经可以由相对属性确定,因此出于与上述情况相同的原因,他们只是将其指定为0dp。 However, I can't seem to find actual documentation on this so it's still a bit confusing.
但是,我似乎找不到有关此内容的实际文档,因此仍然有些混乱。
When a child view is anchored on both sides (in RelativeLayout), the width is ignored. 当子视图固定在两侧(在RelativeLayout中)时,宽度将被忽略。 In this case, the
Spinner
knows it width because of 在这种情况下,
Spinner
知道它的宽度,因为
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times"
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... 我也在几个项目中也看到了这个技巧,其原因与为什么在LinearLayout中使用“ weight”做到这一点完全相同,但是这里的关键属性是:
android:layout_toLeftOf="@+id/times"
与“ weight”相同的技巧将用于线性布局,因此这是一种说法,可以将android:layout_alignParentLeft="true"
剩余的所有空间都使用到android:layout_toLeftOf="@+id/times"
而无需对任何特定的内容进行硬编码宽度...
Regards! 问候!
我们在与layout_weight一起使用时使用0dp,因为它会忽略该属性,并且会按照layout_weight进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.