fill_parent和wrap_content有什么区别?

295

在Android中,布局小部件时,fill_parent (match_parent 在 API Level 8 及以上) 和 wrap_content 有什么区别?

是否有任何可以指向的文档?我想要很好地理解它。


37
请注意,fill_parent 在 API Level 8 及以上已更名为 match_parent - gfrigon
4个回答

277

这两个属性都可以应用于视图(可视化控件)的水平或垂直大小。它用于基于内容或其父布局的大小来设置View或布局的大小,而不是明确指定维度。

fill_parent(在API级别8及更高版本中已弃用并更名为MATCH_PARENT

将小部件的布局设置为fill_parent将强制其扩展到占用放置在其中的布局元素内提供的所有空间。这大致相当于将Windows窗体控件的DockStyle设置为Fill

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

wrap_content

将View的大小设置为wrap_content将强制其仅扩展到足以包含其包含的值(或子控件)。对于控件(如文本框(TextView)或图像(ImageView)),这将包装所显示的文本或图像。对于布局元素,它将调整布局的大小以适应添加为其子项的控件/布局。

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

在线文档

Android代码文档中有一些详细信息,在这里


13
如果图片宽度大于屏幕宽度并且我将ImageView的宽度设置为fill_parent,那么图片会被压缩到屏幕大小吗? - John Watson
@JohnWatson 你找到答案了吗?我也很好奇。 - Rachael
了解所提到的Windows窗体控件的等效属性是很有用的。 - Rempelos
你看到了什么,@JohnWatson?你的故事是什么?答案是什么? - HopefullyHelpful

41

fill_parent(已弃用)= match_parent
子视图的边框扩展到匹配父视图的边框。

wrap_content
子视图的边框紧密地包裹其自身的内容。

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

显示图片

每个 View(一个 TextView,一个 ImageView,一个 Button 等等)都需要设置视图的 widthheight。在 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>

水平线性布局

<?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 或 padding。但即使存在,基本概念仍然相同。视图边框/间距只是由 margin 或 padding 的值进行调整。


14
  • fill_parent会使元素的宽度或高度与父元素(容器)一样大。

  • wrap_content会使宽度或高度根据内容自动适应,以包含其中的元素。

点击此处查看安卓文档参考


什么将是容器?如何用不同的容器包围视图? - Tejzeratul

3

fill_parent

当一个组件被设置为fill_parent布局时,它会强制扩展以填充布局单元成员,尽可能占用空间。这与Windows控件的dockstyle属性一致。将布局或控件设置为fill_parent将强制其占据整个屏幕。

wrap_content

将视图设置为wrap_content大小,将强制视图扩展以显示所有内容。例如,TextView和ImageView控件设置为wrap_content将显示其整个内部文本和图像。布局元素将根据内容更改大小。将视图设置为大小为Autosize属性wrap_content大致相当于将Windows控件设置为True。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接