I'm a little confused about two XML properties: match_parent and fill_parent. It seems that both are the same. Is there any difference between them?
问题:
回答1:
They're the same thing (in API Level 8+). Use match_parent.
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 bymatch_parent.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
回答2:
Google changed the name to avoid confusion.
Problem with the old name fill parent was that it implies its affecting the dimensions of the parent, while match parent better describes the resulting behavior - match the dimension with the parent.
Both constants resolve to -1 in the end, and so result in the identical behavior in the app. Ironically enough, this name change made to clarify things seems to have added confusion rather than eliminating it.
回答3:
Functionally no difference, Google just changed the name from fill_parent to match_parent, from API level 8 (Android 2.2). FILL_PARENT is still available for compatibility reason.
LayoutParams.FILL_PARENT and LayoutParams.MATCH_PARENT both have value -1. Not sure what tempted google to change from Fill Parent to Match Parent :)
Since most of the phones are >= Android 2.2 .. you should use Match Parent for future compatibility... not sure when they will discontinue the older Fill Parent constant!
回答4:
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.
回答5:
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_PARENTis 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"
回答6:
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.
回答7:
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.
回答8:
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"
回答9:
FILL_PARENT is deprecated in API level 8 and MATCH_PARENTuse higherlevel API
回答10:
Both have similar functionality only difference is that fill_parent is used up to API level 8 and match_parent is used after API level 8 or higher level.
回答11:
When you set layout width and height as match_parent in XML property, it will occupy the complete area that the parent view has, i.e. it will be as big as the parent.
Hare parent is red and child is green. Child occupy all area. Because it's width and height are match_parent.
Note : If parent is applied a padding then that space would not be included.
So TextView hight = 300dp(parent hight) - (20(paddingTop)+10(paddingBottom)) = (300 - 30) dp = 270 dp
fill_parent Vs match_parent
fill_parent is previous name of match_parent
For API Level 8 and higher fill_parent renamed as match_parent and fill_parent is deprecated now.
So fill_parent and match_parent are same.
API Documentation for 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 {@code match_parent}.
回答12:
match_parent and fill_parent are same property, used to define width or height of a view in full screen horizontally or vertically.
These properties are used in android xml files like this.
android:layout_width="match_parent" android:layout_height="fill_parent" or
android:layout_width="fill_parent" android:layout_height="match_parent" fill_parent was used in previous versions, but now it has been deprecated and replaced by match_parent. I hope it'll help you.
回答13:
fill_parent: The view should be as big as its parent.
now this content fill_parent is deprecated and replaced by match_parent.
回答14:
To me fill parent and match parent performs the same function only that:
fill parent: Was used before API 8
match parent This was used from API 8+ Function of Both Fills the parent view aside the padding
回答15:
match_parent, which means that the view wants to be as big as its parent (minus padding).
wrap_content, which means that the view wants to be just big enough to enclose its content (plus padding)
For sake of better illustration, I have created a sample layout that demonstrate this concept. To see it's effect, I added a border of each textView content.
In "Match parent" textView content, we can see it's layout width spread out of it's parent whole length.
But we can see in "Wrap Content" textView content, it's layout width wrapped in of it's content(Wrap Content) length.
回答16:
FILL_PARENT was renamed MATCH_PARENT in API Level 8 and higher which means that the view wants to be as big as its parent (minus padding) - Google
回答17:
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?
As 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
回答18:
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.
Be current. Be clear. Use MATCH_PARENT.
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.
And this is what it looks when run on a device.
Woah, you set that red View to MATCH_PARENT and it filled the remaining space in the LinearLayout. It's not as big as its parent. What gives?
And here's the confusion. LinearLayout lays out its children sequentially, the start position of a child being the end position of the previous child. A child requests that the parent give it certain dimensions, but this does not effect where the child is positioned. Since the red View is third, it is laid out after the first two, and thus, ends up not as "big as parent."
This is a consequence of position, not of how MATCH_PARENT/FILL_PARENT works. The LinearLayout honors the requests of its child Views the best it can, but cannot guarantee them.
Now, let us see what happens when we switch things around by making the red View first in the layout:
Now here is what it looks like, and it is easy to see the difference. Since the LinearLayout lays out the red View first, it positions it at the top and can fully meet its MATCH_PARENT request. That results in the red View pushing the other two out of sight.
Okay, so what if I actually want to make a View fill the remaining space in its parent?
HOW TO ACTUALLY "FILL PARENT The general strategy for implementing "fill parent" is to use a LinearLayout and the layout_weight attribute. Now we will use them to fix our second layout that was taken over by the Blob (that red View).
Here we were able to keep the red View as the first child in the LinearLayout and have it only fill the space remaining after laying out the other two views.
Really there is never just one way to create a layout. If we wanted to just make the red View last in the LinearLayout and have it fill the remaining space, we could have used the first layout, end of story. However, it is important to understand why it works and when it might not work.