fill_parent和wrap_content有什么区别?

pup*_*eno 271 layout user-interface android

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

你有什么文件可以指出吗?我很有兴趣了解它.

Ret*_*ier 261

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

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

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

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

wrap_content

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

它大致相当于将Windows Form Control的Autosize属性设置为True.

在线文档

有Android的代码,文档中的一些细节在这里.

  • 如果图像宽度大于屏幕宽度,我将imageview宽度设置为fill_parent.将图像压缩到屏幕大小? (12认同)

Sur*_*gch 34

fill_parent(不建议使用)=match_parent
子视图的边框将展开以匹配父视图的边框.

wrap_content
子视图的边界紧紧围绕着自己的内容.

这里有一些图片可以让事情更加清晰.绿色和红色是TextViews.白色是LinearLayout透视.

在此输入图像描述

每个View(a TextView,an ImageView,a Button等)都需要设置视图widthheight视图.在xml布局文件中,可能如下所示:

android:layout_width="wrap_content"
android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)

除了将宽度和高度设置为match_parent或之外wrap_content,您还可以将它们设置为某个绝对值:

android:layout_width="100dp"
android:layout_height="200dp"
Run Code Online (Sandbox Code Playgroud)

但是,通常情况并不好,因为它不适合不同尺寸的设备.你明白后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>
Run Code Online (Sandbox Code Playgroud)

水平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>
Run Code Online (Sandbox Code Playgroud)

注意

本回答中的解释假设没有边距或填充.但即使有,基本概念仍然是相同的.视图边框/间距仅通过边距或填充的值进行调整.


Dev*_*ath 9

  • fill_parent 将使元素的宽度或高度与父元素一样大,换句话说,就是容器.

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

单击此处获取ANDROID DOC参考