What's the difference between fill

In Android, when layout out widgets, what's the difference between fill_parent ( match_parent in API Level 8 and higher) and wrap_content ?

Is there any documentation where you can point to? I'm interested in understanding it very well.


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)

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 .

Setting a top level layout or control to fill_parent will force it to take up the whole screen.

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.

It's roughly the equivalent of setting a Windows Form Control's Autosize property to True.

Online Documentation

There's some details in the Android code documentation here.


  • FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the View wants to be as big as its parent (minus padding)

  • WRAP_CONTENT , which means that the View wants to be just big enough to enclose its content (plus padding)


  • fill_parent (deprecated) = match_parent
    The border of the child view expands to match the border of the parent view.

    wrap_content
    The border of the child view wraps snugly around its own content.

    Here are some images to make things more clear. The green and red are TextViews . The white is a LinearLayout showing through.

    在这里输入图像描述

    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:

    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:

    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 .

    See also

  • What does android:layout_weight mean?
  • Difference between a View's Padding and Margin
  • Gravity vs layout_gravity
  • XML for above images

    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.

    链接地址: http://www.djcxy.com/p/84740.html

    上一篇: 动态地将视图添加到视图中

    下一篇: 填充有什么区别