ChatGPT解决这个技术问题 Extra ChatGPT

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

我发现 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 中有两个做同样事情的方法是弃用一个方法的原因之一。
有关此stackoverflow.com/questions/5761960/…的更多信息

R
Robert Martin

正如你所说,它们完全相同。正如 Romain Guy 所说,他们更改了名称是因为 "fill_parent" 让开发人员感到困惑。事实上,"fill_parent" 不会填充剩余空间(因为您使用 weight 属性),但它占用的空间与其布局父级一样多。这就是新名称为 "match_parent" 的原因。


Z
Zsombor Erdődy-Nagy

根据 this 视频中的 Romain Guy 所说,这些词标志着相同的行为。但是很多开发者误解了 fill_parent 的意思,因此他们想出了一个别名。


s
stacksonstacksonstackoverflow

我已经在 Android 中开发了足够长的时间,也意识到似乎没有什么区别,除非您想在较旧的 API 上运行。我会使用 fill_parent,因为我使用最低 API 7 制作所有应用程序。另外,由于 Android 是向前兼容的,所以这是要走的路。


m
mes

除了现有的答案。这是来自 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;
    ...