Android布局文件中wrap_content和0dp的区别-飞
发布时间: 2023-07-06
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="w(spc是什么意思?spc即统计过程控制(Statistical Process Control),是一种借助数理统计方法的过程控制工具。)rap_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_he

微信