Android 什么';填充父内容和换行内容之间的区别是什么?

Android 什么';填充父内容和换行内容之间的区别是什么?,android,layout,user-interface,Android,Layout,User Interface,在安卓系统中,当布局小部件时,fill\u parent(match\u parent在API级别8及更高级别)和wrap\u content之间有什么区别 是否有您可以指向的文档?我很想很好地理解它。任一属性都可以应用于视图(视觉控制)的水平或垂直大小。它用于根据视图的内容或父布局的大小设置视图或布局大小,而不是显式指定尺寸 fill\u parent(API级别8及更高版本中已弃用并重命名为MATCH\u parent) 将小部件的布局设置为填充父项将迫使其展开,以在其所在的布局元素中占据尽

在安卓系统中,当布局小部件时,
fill\u parent
match\u parent
在API级别8及更高级别)和
wrap\u content
之间有什么区别


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

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


fill\u parent
(API级别8及更高版本中已弃用并重命名为
MATCH\u parent

将小部件的布局设置为填充父项将迫使其展开,以在其所在的布局元素中占据尽可能多的可用空间。这大致相当于将Windows窗体控件的dockstyle设置为
Fill

将顶层布局或控件设置为填充父项将强制它占据整个屏幕

包装内容

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

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

在线文档

Android代码文档中有一些细节

  • fill\u parent
    将使元素的宽度或高度为 与父元素(换句话说,容器)一样大

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


填充父项

组件布局为
填充\u父级
必须展开以尽可能多地填充空间中的布局单元成员。这与Windows控件的dockstyle属性一致。顶部设置布局或控件以填充父对象将强制其占据整个屏幕

包装内容

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


有关详细信息,请查看此链接:

填充父项
(已弃用)=
匹配父项

子视图的边框将展开以匹配父视图的边框

包装内容

子视图的边框紧密地围绕着它自己的内容

下面是一些图片,让事情更清楚。绿色和红色是
文本视图
。白色是一个
线性布局
显示通过

每个
视图
(一个
文本视图
,一个
图像视图
,一个
按钮
,等等)都需要设置视图的
宽度
高度
。在xml布局文件中,可能如下所示:

android:layout_width="wrap_content"
android:layout_height="match_parent"
除了将宽度和高度设置为
match_parent
wrap_content
,还可以将其设置为某个绝对值:

android:layout_width="100dp"
android:layout_height="200dp"
不过,一般来说,这并没有那么好,因为对于不同大小的设备,它没有那么灵活。在理解了
包装内容
匹配父项
之后,接下来要学习的是
布局权重

另见
用于上述图像的XML 垂直线性布局

<?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>

水平线性布局

<?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>


这个答案中的解释假设没有。但即使有,基本概念仍然是一样的。视图边框/间距仅通过边距或填充的值进行调整。

请注意,
fill\u parent
在API Level 8及更高版本中被重命名为
match\u parent
。如果图像宽度大于屏幕宽度,并且我将imageview width设置为fill\u parent,该怎么办。图像会被压缩到屏幕大小吗?@JohnWatson你找到答案了吗?我也很好奇。很高兴知道上面提到的Windows窗体控件的等效属性。你在@JohnWatson看到了什么?你的故事是什么?答案是什么?容器将是什么?如何用不同的容器包围视图?