Вопрос: В чем разница между match_parent и fill_parent?


Я немного запутался в двух свойствах XML: match_parentа также fill_parent, Кажется, что оба они одинаковы. Есть ли разница между ними?


1246


источник


Ответы:


Они - то же самое (в Уровне 8+ API). использование match_parent,

FILL_PARENT (переименованный MATCH_PARENT в API 8 и выше), что означает, что представление хочет быть таким же большим, как и его родительский (минус заполнение)

...

fill_parent: Представление должно быть таким же большим, как и его родительское (минус заполнение). Эта константа устарела, начиная с уровня API 8 и заменяется на match_parent,

http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html


1148



Google изменило имя, чтобы избежать путаницы.

Проблема со старым именем fill parentзаключалось в том, что оно подразумевает его влияние на размеры родителя, тогда как match parentлучше описывает результирующее поведение - соответствует размеру с родителем.

Обе константы разрешают -1в конце концов, и, следовательно, приводят к одинаковому поведению в приложении. Как ни странно, это изменение имени, сделанное для выяснения вещей, похоже, добавило путаницы, а не устранило ее.


225



Функционально никакой разницы, Google только что изменил имя от fill_parent до match_parent, с уровня API 8 (Android 2.2). FILL_PARENT по-прежнему доступен по соображениям совместимости.

LayoutParams.FILL_PARENTа также LayoutParams.MATCH_PARENTоба имеют значение -1. Не уверен, что соблазнил google для перехода от Fill Parent к Match Parent :)

Поскольку большая часть phones are >= Android 2.2.. вы должны использовать Match Parent для будущей совместимости ... не уверены, когда они прекратят использование старой константы Fill Parent!


22



For compatibility sake it's better to stick to fill_parent, i.e, when supporting below API 8 devices. But if your app targets API 8 and upwards, you should use match_parent instead.


10



fill_parent (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)

fill_parent: The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent

For Android API 1.6 to 2.1 match_parent will throw you an error, so use fill_parent in these cases. To support backward compatibility, it's better to use fill_parent

  • Both are static final constants that represent the value -1
  • FILL_PARENT is deprecated in API level 8

So MATCH_PARENT and FILL_PARENT are the same?

Yes, MATCH_PARENT and FILL_PARENT are just different constant names for the same integer value (-1 if you are curious) used to specify a View's layout mode within its parent.

So why was MATCH_PARENT added?

I remember that Roman Guy (Android Developer at Google) said, that they have changed the name because "fill_parent" was confusing for developers. As matter of the fact, "fill_parent" does not fill the remaining space (for that you use the weight attribute) but it takes as much space as its layout parent. That's why the new name is "match_parent"


10



MATCH_PARENT and FILL_PARENT are the same?

Yes, MATCH_PARENT and FILL_PARENT are just different constant names for the same integer value (-1 if you are curious) used to specify a View's layout mode within its parent.

So why was MATCH_PARENT added?

The Android team found that developers were misinterpreting FILL_PARENT to mean that a View would fill the remaining space left in its parent. In fact, by specifying FILL_PARENT, the View is requesting to be as big as its parent. Thus, (as explained by Romain Guy himself in this video at around 10:56) the constant was renamed MATCH_PARENT to clarify its use.

Okay, I get how FILL_PARENT/MATCH_PARENT works. What does it matter if I use one or the other?

FILL_PARENT is deprecated. Being deprecated does not make it the Devil, but eventually it will go away. Your application is more future-proof using MATCH_PARENT. Why use the deprecated option when the current option behaves exactly the same?

Furthermore, FILL_PARENT is actually misleading. I have personally seen folks confused because it does not work the way they thought it would. In fact, I have seen a single tag using both FILL_PARENT and MATCH_PARENT, because the developer thought (understandably) that they were two different layout modes.

HOW YOU CAN GET TRIPPED UP TRYING TO FILL_PARENT?

Here's a quick example of how MATCH_PARENT/FILL_PARENT confusion can lead to folks repeatedly applying their heads to their desks. First, take a really simple layout containing a few View instances.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FFFF00" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@android:color/black"
        android:text="@string/hello_world"
        android:padding="50dp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000" />

</LinearLayout>

6



FILL_PARENT is deprecated in API level 8 and MATCH_PARENTuse higherlevel API


5



match_parent is used in place of fill_parent and sets it to go as far as the parent goes. Just use match_parent and forget about fill_parent. I completely ditched fill_parent and everything is perfect as usual.

Check here for more.


5



Just to give it a name closer to it's actual action. "fill_parent" does not fill the remaining space as the name would imply (for that you use the weight attribute). Instead, it takes up as much space as its layout parent. That's why the new name is "match_parent"


4