填充有什么区别

在Android中,布局小部件时, fill_parent (API Level 8中的match_parent和更高级别)和wrap_content之间有什么区别?

有没有可以指向的文档? 我很想理解它。


任何一个属性都可以应用到View的(可视控制)水平或垂直尺寸。 它用于根据其内容或其父级布局的大小设置视图或布局大小,而不是明确指定维度。

fill_parent (在API级别8和更高版本中弃用fill_parent命名MATCH_PARENT

将小部件的布局设置为fill_parent将强制扩展,以占用其放置的布局元素中可用空间的大小。这大致等同于将Windows窗体控件的dockstyle设置为Fill

将顶级布局或控件设置为fill_parent将强制它占据整个屏幕。

wrap_content

将视图的大小设置为wrap_content将强制它扩展得足够远以包含它所包含的值(或子控件)。 对于控件 - 如文本框(TextView)或图像(ImageView) - 这将包装显示的文本或图像。 对于布局元素,它将调整布局大小以适合添加为其子元素的控件/布局。

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

在线文档

这里有一些Android代码文档中的细节。


  • FILL_PARENT (在API级别8和更高版本中重命名为MATCH_PARENT ),这意味着该视图的大小与其父级(减去填充)相同,

  • WRAP_CONTENT ,这意味着视图要足够大以包含其内容(加上填充)


  • fill_parent (不赞成) = match_parent
    子视图的边框将展开以匹配父视图的边框。

    wrap_content
    子视图的边框包裹着它自己的内容。

    这里有一些图片可以让事情更加清晰。 绿色和红色是TextViews 。 白色是一个LinearLayout显示。

    在这里输入图像描述

    每个View (一个TextView ,一个ImageView ,一个Button等)需要设置视图的widthheight 。 在xml布局文件中,可能如下所示:

    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    

    除了将宽度和高度设置为match_parentwrap_content ,还可以将它们设置为某个绝对值:

    android:layout_width="100dp"
    android:layout_height="200dp"
    

    一般来说,这并不是很好,因为它不适合不同大小的设备。 在理解了wrap_contentmatch_parent ,接下来要学习的是layout_weight

    也可以看看

  • android:layout_weight是什么意思?
  • 视图的填充和边距之间的区别
  • 重力与layout_gravity
  • XML用于以上图像

    垂直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>
    

    水平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>
    

    注意

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

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

    上一篇: What's the difference between fill

    下一篇: TableLayout Crashing application on android Lollipop