简体   繁体   English

在RelativeLayout中将layout_width(height)设置为0dp

[英]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.

相关问题 如果在使用layout_weight属性时未为layout_width或layout_height指定0dp会发生什么? - What happens when we dont specify 0dp for layout_width or layout_height when using layout_weight attribute? linearlayout中的layout_weight和layout_width = 0dp创建意外的布局 - layout_weight and layout_width = 0dp in a linearlayout create an unexpected layout Android GridLayout layout_columnWeight="1" 和 layout_width="0dp" 以编程方式 - Android GridLayout layout_columnWeight="1" and layout_width="0dp" programmatically 为什么 UI Design Builder 中的“Infer Constraints”将某些视图的布局高度和宽度设置为 0dp? - Why does "Infer Constraints" in the UI Design Builder set the layout height and width of some views to 0dp? 没有`weight`属性时android:layout_width =“ 0dp”是什么意思? - what is the meaning of android:layout_width=“0dp” when there is not `weight` attribute? weight=1 和 android:layout_width=“0dp” 使我的视图消失 - weight=1 and android:layout_width=“0dp” makes my view disappear 约束布局的宽度和高度属性自动重置为0dp - Constraint layout's width and height attributes automatically reset to 0dp 嵌套的layout_weight不利于性能:将layout_width值设置为0dp时,为什么会出现Lint警告? - Nested layout_weight bad for performance: Why do I get Lint warning when I set layout_width value to 0dp? 当宽度和高度设置为 0dp 时,回收站视图不呈现 - Recycler view is not rendering when width & height are set to 0dp 将高度设置为0dp时布局权重不起作用 - layout weight doesn't work when set the height to 0dp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM