不赞成使用的单词是fill_parent和match_parent之间的唯一区别


70

我发现,无论是fill_parentmatch_parent意味着同样的事情

  • fill_parent表示视图要与父视图一样大,减去父视图的填充(如果有)。
  • match_parent表示视图要与父视图一样大,减去父视图的填充(如果有)。

我发现的唯一区别是,fill_parent从API级别8开始不推荐使用,而是将其替换为match_parent

但是,我没有注意到两者之间的任何区别。如果两者相同,那么为什么fill_parent不推荐使用。任何人都可以解释这两者之间的任何区别,但一个不赞成,另一个不反对?

我已经浏览了http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html


API中有两个执行相同功能的方法是弃用一个方法的原因之一。
FYR

Answers:


91

正如您所说,它们是完全一样的。正如罗曼·盖伊(Romain Guy)所说,他们之所以改名,是因为"fill_parent"它使开发人员感到困惑。实际上,"fill_parent"不会填充剩余空间(为此您使用weight属性),但是会占用与其布局父级一样多的空间。这就是为什么新名称是"match_parent"


9

据罗曼·盖伊(Romain Guy)在这段视频中表示,这些词表示相同的行为。但是许多开发人员误解了fill_parent的含义,因此他们想出了一个别名。


7

我已经在Android上进行了足够长的开发,以意识到除了希望在较旧的API上运行之外,似乎没有什么区别。我fill_parent之所以使用,是因为我用最低的API 7来制作所有应用程序。此外,顺便说一句,由于Android是向前兼容的,因此这是必须的方法。


4

除了现有答案。这是LayoutParams该类源代码的一部分,FILL_PARENT和MATCH_PARENT常量都映射到相同的值。因此,我们具有完全相同的功能。

    public static class LayoutParams {
    /**
     * Special value for the height or width requested by a View.
     * FILL_PARENT means that the view wants to be as big as its parent,
     * minus the parent's padding, if any. This value is deprecated
     * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
     */
    @SuppressWarnings({"UnusedDeclaration"})
    @Deprecated
    public static final int FILL_PARENT = -1;

    /**
     * Special value for the height or width requested by a View.
     * MATCH_PARENT means that the view wants to be as big as its parent,
     * minus the parent's padding, if any. Introduced in API Level 8.
     */
    public static final int MATCH_PARENT = -1;
    ...
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.