美文网首页Android控件使用篇Android专题
Android布局文件中wrap_content和0dp的区别

Android布局文件中wrap_content和0dp的区别

作者: 千夜零一 | 来源:发表于2021-03-18 10:04 被阅读0次

    Layout下的布局设置Widget宽高的填充形式:

    (1)match_parent:指占满父容器此时要控件的宽或高等于父容器的宽或高。
    (2)wrap_content和的用法:指控件的高或宽随内容的长度决定。
    (3)设置固定值,可以是30dp,也可以是120dp,想要设置为0dp,必须有weight属性,且值不为0才可以。


    不同布局效果

    (1)第一种情况:

    <LinearLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    效果图:

    case1.png

    (2)第二种情况:

    <LinearLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    效果图:

    case2.png

    (3)第三种情况:

    <LinearLayout
       android:background="@color/white"
       android:layout_width="match_parent"
       android:layout_height="100dp"
       android:orientation="horizontal">
       <Button
           android:layout_width="wrap_content"
           android:layout_height="match_parent"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
    </LinearLayout>
    

    效果图:

    case3.png

    (4)第四种情况:

    <LinearLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
    

    效果图:

    case4.png

    (5)第五种情况:

    <LinearLayout
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal">
            <Button
                android:text="Btn1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <Button
                android:text="Btn2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    

    效果图:

    case5.png

    (6)第六种情况:

    <LinearLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button
            android:text="Btn1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <Button
            android:text="Btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    效果图:

    case6.png

    设置比重时需要改为0dp的问题

    而当我们使用到比重的时候,会在代码中有提示:让我们将layout_width的值设置为0dp


    casedemo.png

    设置之后:

    <LinearLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    效果图:均分铺满

    case7.png

    而如果我们的布局如下:将layout_width的值设置为wrap_content

    <LinearLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    那么效果图:也是均分铺满

    case8.png

    那么问题来了,到底为什么代码提示我们需要将layout_width的值设置为0dp呢?

    答案:事实是这样的,系统会先根据width和height属性首次对控件进行排版,然后在查看是否分配了权重,在按照权重二次分配控件。这样就执行了两次。如果将width(或height)为0dp就执行一次就可以了,提高运行性能。避免重复排版。

    相关文章

      网友评论

        本文标题:Android布局文件中wrap_content和0dp的区别

        本文链接:https://www.haomeiwen.com/subject/ndlucltx.html