fill_parent(已弃用)= match_parent
子视图的边框扩展以匹配父视图的边框。
wrap_content
子视图的边框紧紧地包裹在它自己的内容周围。
这里有一些图片可以让事情更清楚。绿色和红色是TextViews。白色是LinearLayout 透出。
每个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。
另见
以上图片的 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。但即使有,基本概念还是一样的。视图边框/间距仅通过边距或内边距的值进行调整。