android 代码布局设置wrap_content,Android布局文件中wrap_content和0dp的区别

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

(1)match_parent:指占满父容器此时要控件的宽或高等于父容器的宽或高。

(2)wrap_content和的用法:指控件的高或宽随内容的长度决定。

(3)设置固定值,可以是30dp,也可以是120dp,想要设置为0dp,必须有weight属性,且值不为0才可以。

不同布局效果

(1)第一种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case1.png

(2)第二种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case2.png

(3)第三种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="match_parent"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case3.png

(4)第四种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="match_parent"/>

效果图:

9836694dace1

case4.png

(5)第五种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:text="Btn1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:text="Btn2"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case5.png

(6)第六种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:text="Btn1"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content" />

android:text="Btn2"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case6.png

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

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

9836694dace1

casedemo.png

设置之后:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content" />

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"/>

效果图:均分铺满

9836694dace1

case7.png

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

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="wrap_content"/>

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

9836694dace1

case8.png

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值