fill_parent和wrap_content有什么区别?

时间:2009-01-11 11:14:07

标签: android layout user-interface

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

您可以指出哪些文档?我很有兴趣了解它。

4 个答案:

答案 0 :(得分:252)

任何一个属性都可以应用于View的(可视控件)水平或垂直大小。它用于根据内容或其父布局的大小设置视图或布局大小,而不是显式指定维度。

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

将窗口小部件的布局设置为fill_parent将强制它扩展以占用放置它的布局元素中可用的空间。这大致相当于将Windows窗体控件的dockstyle设置为{{1 }}。

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

<强> Fill

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

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

在线文档

Android代码文档here中有一些详细信息。

答案 1 :(得分:31)

fill_parent (已弃用) = match_parent
子视图的边框将展开以匹配父视图的边框。

<强> wrap_content
子视图的边界紧紧围绕着自己的内容。

以下是一些让事情更加清晰的图片。绿色和红色是TextViews。白色显示为LinearLayout

enter image description here

每个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>

水平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。但即使有,基本概念仍然是相同的。视图边框/间距只是通过边距或填充的值来调整。

答案 2 :(得分:9)

  • fill_parent将使元素的宽度或高度为 large作为父元素,换句话说就是容器。

  • wrap_content将使宽度或高度尽可能大 包含其中的元素。

Click here for ANDROID DOC Reference

答案 3 :(得分:2)

fill_parent

组件布局为fill_parent将必须扩展以填充布局单元成员,尽可能在空间中。这与Windows控件的dockstyle属性一致。对fill_parent的顶部布局或控件将强制它占据整个屏幕。

<强> wrap_content

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

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