在 Android 中,布局小部件时,fill_parent
(API 级别 8 及更高级别中的 match_parent
)和 wrap_content
有什么区别?
有没有可以指向的文档?我有兴趣很好地理解它。
fill_parent
已重命名为 match_parent
。
任一属性都可以应用于 View 的(视觉控制)水平或垂直尺寸。它用于根据其内容或其父布局的大小设置视图或布局大小,而不是显式指定维度。
fill_parent
(在 API 级别 8 及更高版本中已弃用并重命名为 MATCH_PARENT
)
将小部件的布局设置为 fill_parent 将强制它展开以占用其所在布局元素中可用的空间。这大致相当于将 Windows 窗体控件的停靠样式设置为 Fill
。
将顶级布局或控件设置为 fill_parent 将强制它占据整个屏幕。
wrap_content
将 View 的大小设置为 wrap_content 将强制它仅扩展到足以包含它所包含的值(或子控件)的程度。对于控件——如文本框 (TextView) 或图像 (ImageView)——这将包装正在显示的文本或图像。对于布局元素,它将调整布局大小以适应作为其子元素添加的控件/布局。
它大致相当于将 Windows 窗体控件的 Autosize
属性设置为 True。
在线文档
Android 代码文档 here 中有一些详细信息。
fill_parent
(已弃用)= match_parent
子视图的边框扩展以匹配父视图的边框。
wrap_content
子视图的边框紧贴其自身的内容。
这里有一些图像可以使事情更清楚。绿色和红色是TextViews
。白色是一个 LinearLayout
透出。
https://i.stack.imgur.com/9Xhsl.png
每个 View
(TextView
、ImageView
、Button
等)都需要设置视图的 width
和 height
。在 xml 布局文件中,可能如下所示:
android:layout_width="wrap_content"
android:layout_height="match_parent"
除了将宽度和高度设置为 match_parent
或 wrap_content
之外,您还可以将它们设置为某个绝对值:
android:layout_width="100dp"
android:layout_height="200dp"
但是,通常这不是很好,因为它对于不同尺寸的设备没有那么灵活。理解了wrap_content
和match_parent
之后,接下来要学习的是layout_weight
。
也可以看看
android:layout_weight 是什么意思?
视图的填充和边距之间的区别
重力与 layout_gravity
上述图像的 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>
笔记
此答案中的解释假设没有 margin or padding。但即使有,基本概念还是一样的。视图边框/间距仅通过边距或填充的值进行调整。
fill_parent 将使元素的宽度或高度与父元素(即容器)一样大。
wrap_content 将使宽度或高度与包含其中的元素一样大。
Click here for ANDROID DOC Reference
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
不定期副业成功案例分享