Android fill_parent 和 wrap_content 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/432763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between fill_parent and wrap_content?
提问by pupeno
In Android, when layout out widgets, what's the difference between fill_parent
(match_parent
in API Level 8 and higher) and wrap_content
?
在 Android 中,当布局小部件时,fill_parent
(match_parent
在 API 级别 8 及更高版本中)和wrap_content
?
Is there any documentation where you can point to? I'm interested in understanding it very well.
是否有任何可以指向的文档?我有兴趣很好地理解它。
采纳答案by Reto Meier
Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.
任一属性都可以应用于视图的(视觉控制)水平或垂直大小。它用于根据其内容或其父布局的大小设置视图或布局大小,而不是明确指定尺寸。
fill_parent
(deprecated and renamed MATCH_PARENT
in API Level 8 and higher)
fill_parent
(MATCH_PARENT
在 API 级别 8 及更高版本中已弃用并重命名)
Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill
.
将小部件的布局设置为 fill_parent 将强制它扩展以占据放置它的布局元素内可用的空间。这大致相当于将 Windows 窗体控件的停靠样式设置为Fill
。
Setting a top level layout or control to fill_parent will force it to take up the whole screen.
将顶级布局或控件设置为 fill_parent 将强制它占据整个屏幕。
wrap_content
wrap_content
Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.
将视图的大小设置为 wrap_content 将强制它扩展到足以包含它包含的值(或子控件)。对于控件——比如文本框 (TextView) 或图像 (ImageView)——这将包裹显示的文本或图像。对于布局元素,它将调整布局大小以适应作为其子项添加的控件/布局。
It's roughly the equivalent of setting a Windows Form Control's Autosize
property to True.
这大致相当于将 Windows 窗体控件的Autosize
属性设置为 True。
Online Documentation
在线文档
There's some details in the Android code documentation here.
此处的 Android 代码文档中有一些详细信息。
回答by Suragch
fill_parent
(deprecated) = match_parent
The border of the child view expands to match the border of the parent view.
fill_parent
(已弃用)=match_parent
子视图的边框扩展以匹配父视图的边框。
wrap_content
The border of the child view wraps snugly around its own content.
wrap_content
子视图的边框紧紧地包裹在它自己的内容周围。
Here are some images to make things more clear. The green and red are TextViews
. The white is a LinearLayout
showing through.
这里有一些图片可以让事情更清楚。绿色和红色是TextViews
。白色是LinearLayout
透出的。
Every View
(a TextView
, an ImageView
, a Button
, etc.) needs to set the width
and the height
of the view. In the xml layout file, that might look like this:
每个View
(a TextView
、 an ImageView
、 aButton
等)都需要设置视图的width
和height
。在 xml 布局文件中,它可能如下所示:
android:layout_width="wrap_content"
android:layout_height="match_parent"
Besides setting the width and height to match_parent
or wrap_content
, you could also set them to some absolute value:
除了将宽度和高度设置为match_parent
or 之外wrap_content
,您还可以将它们设置为某个绝对值:
android:layout_width="100dp"
android:layout_height="200dp"
Generally that is not as good, though, because it is not as flexible for different sized devices. After you have understood wrap_content
and match_parent
, the next thing to learn is layout_weight
.
但是,通常这不是那么好,因为它对于不同尺寸的设备没有那么灵活。在你理解了wrap_content
和之后match_parent
,接下来要学习的是layout_weight
。
See also
也可以看看
- What does android:layout_weight mean?
- Difference between a View's Padding and Margin
- Gravity vs layout_gravity
XML for above images
以上图像的 XML
Vertical 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>
Horizontal 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>
Note
笔记
The explanation in this answer assumes there is no margin or padding. But even if there is, the basic concept is still the same. The view border/spacing is just adjusted by the value of the margin or padding.
此答案中的解释假定没有margin 或 padding。但即使有,基本概念还是一样的。视图边框/间距仅通过边距或填充的值进行调整。
回答by Devrath
fill_parent
will make the width or height of the element to be as large as the parent element, in other words, the container.wrap_content
will make the width or height be as large as needed to contain the elements within it.
fill_parent
将使元素的宽度或高度与父元素(即容器)一样大。wrap_content
将使宽度或高度尽可能大以包含其中的元素。
回答by IntelliJ Amiya
fill_parent
:
fill_parent
:
A component is arranged layout for the fill_parent
will be mandatory to expand to fill the layout unit members, as much as possible in the space. This is consistent with the dockstyle property of the Windows control. A top set layout or control to fill_parent
will force it to take up the entire screen.
一个组件被布置为布局时fill_parent
会强制展开以填充布局单元中的成员,尽可能多的空间。这与Windows 控件的dockstyle 属性一致。顶部设置布局或控件fill_parent
将强制它占据整个屏幕。
wrap_content
wrap_content
Set up a view of the size of wrap_content
will be forced to view is expanded to show all the content. The TextView and ImageViewcontrols, for example, is set to wrap_content
will display its entire internal text and image. Layout elements will change the size according to the content. Set up a view of the size of Autosize attribute wrap_content
roughly equivalent to set a Windows control for True.
设置视图的大小wrap_content
将强制视图扩展以显示所有内容。所述的TextView和ImageView的控制,例如,被设置为wrap_content
将显示其整个内部的文本和图像。布局元素会根据内容改变大小。设置一个视图的大小 Autosize 属性wrap_content
大致相当于设置一个 Windows 控件为 True。
For details Please Check out this link : http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
详情请查看此链接:http: //developer.android.com/reference/android/view/ViewGroup.LayoutParams.html