2009-01-11 23 views
236

在Android中,布局小部件时,fill_parent(API等级8及更高版本中的match_parent)与wrap_content之间的区别是什么?fill_parent和wrap_content有什么区别?

有没有可以指向的文档?我很想理解它。

+26

请注意,fill_parent在API级别8和更高版本中已重命名为match_parent。 – gfrigon 2012-06-22 18:20:43

回答

238

任一属性都可以应用于视图的(可视控制)水平或垂直尺寸。它用于根据其内容或其父级布局的大小设置视图或布局大小,而不是明确指定维度。

fill_parent(弃用,在API级别8改名为MATCH_PARENT及更高版本)

设置一个小部件的布局FILL_PARENT将迫使它扩大到占据尽可能多的空间是它的布局元素中可用被放置在中。这大致等同于将Windows窗体控件的dockstyle设置为Fill

将顶层布局或控件设置为fill_parent将强制它占据整个屏幕。

wrap_content

设置视图的尺寸为wrap_content将迫使它扩大只是远远不够,以遏制它所包含的值(或子控件)。对于控件 - 如文本框(TextView)或图像(ImageView) - 这将包装显示的文本或图像。对于布局元素,它将调整布局大小以适合添加为其子元素的控件/布局。

这大致相当于将Windows窗体控件的Autosize属性设置为True。

在线文档

有Android的代码文档here在一些细节上。

+12

如果图像宽度大于屏幕宽度,我将imageview宽度设置为fill_parent。将图像压缩到屏幕尺寸? – 2012-08-10 11:18:47

+0

@JohnWatson你有没有找到你的答案?我也很好奇。 – Rachael 2015-09-18 16:48:46

+0

很高兴知道所提及的Windows窗体控件的等效属性。 – Rempelos 2016-01-25 02:46:55

51
  • FILL_PARENT,这意味着 的观想那样大 其父(减去填充)

  • WRAP_CONTENT,这意味着(在 API 8级及以上改名为MATCH_PARENT)的 观想只是大足以 附上其内容(加垫)

8
  • fill_parent将使元素的宽度或高度为 大,如父元素,换句话说,容器。

  • wrap_content将使宽度或高度达到所需的最大值,其中包含其中的元素。

Click here for ANDROID DOC Reference

2

fill_parent

fill_parent

A组分布置布局将强制在该空间扩展以填充所述布局单元部件,尽可能。这与Windows控件的dockstyle属性一致。顶层布局或控制到fill_parent将强制它占据整个屏幕。

wrap_content

设立wrap_content大小的视图将被迫视图中展开,以显示所有的内容。例如,将TextView和ImageView控件设置为wrap_content将显示其整个内部文本和图像。布局元素将根据内容更改大小。设置Autosize属性wrap_content大小的视图大致等同于为True设置Windows控件。

详情请查看此链接:http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

1

WRAP_CONTENT设置视图的大小与minimum required to contain the contents it displays.

match_parentexpands查看to match the available space within the parent View, Fragment, or Activity.

27

fill_parent(不推荐) = match_parent
子视图的边框将展开以匹配父视图的边框。

wrap_content
子视图的边框包裹紧密围绕其自己的内容。

下面是一些图像,使事情更清晰。绿色和红色是TextViews。白色是LinearLayout显示通过。

enter image description here

View(一个TextView,一个ImageView,一个Button等)需要设置width和视图的height。在XML布局文件,这可能是这样的:

android:layout_width="wrap_content" 
android:layout_height="match_parent" 

除了设置宽度和高度match_parentwrap_content,你也可以将它们设置为某个绝对值:

android:layout_width="100dp" 
android:layout_height="200dp" 

一般是尽管如此,这并不是很好,因为它不适合不同大小的设备。在您了解了wrap_contentmatch_parent之后,接下来要学习的内容是layout_weight

参见

XML为上述图像

垂直的LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="width=wrap height=wrap" 
     android:background="#c5e1b0"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="width=match height=wrap" 
     android:background="#f6c0c0"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="width=match height=match" 
     android:background="#c5e1b0"/> 

</LinearLayout> 

水平的LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="WrapWrap" 
     android:background="#c5e1b0"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="WrapMatch" 
     android:background="#f6c0c0"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="MatchMatch" 
     android:background="#c5e1b0"/> 

</LinearLayout> 

在这个答案的解释假定没有margin or padding。但即使存在,基本概念仍然是一样的。视图边框/间距仅由边距或填充值进行调整。