我是靠谱客的博主 深情向日葵,最近开发中收集的这篇文章主要介绍了解界面布局layout_weight属性,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

从代码讲起:

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button3"
/>
</LinearLayout>
复制代码

效果图如下:

我们可以发现这三个button并没有将整个屏幕占据,而是根据内容适配大小。

我们在button2里面添加一个属性  android:layout_weight="1" ,发现布局变成这样了:

我们发现这次屏幕被三个button占据了,而且第一个和第三个button还是保持与内容适配大小,而第二个button而占据了剩余屏幕空间。

我们对上面的情况进行讲解:

1、我们只有在button2使用了layout_weight属性,并赋值为1;但是button1和button2并没有使用这个属性,根据API可以知道,他们的layout_weight属性等于0。

2、LinearLayout如果显式包含layout_weight属性时,会measure两次;第一次将正常计算三个button的宽高,第二次将结合layout_weight的值分配剩余的空间。

  通俗点来总结:Android系统先按照你设置的3个Button高度Layout_height=wrap_content,给你分配好他们3个的高度,然后会把剩下来的屏幕空间全部赋给Button2,因为只有他的权重值是1,这也是为什么Button2占了那么大的一块空间。

这么讲解大概大家也对layout_weight属性有了比较清晰的认识了吧。

最后

以上就是深情向日葵为你收集整理的了解界面布局layout_weight属性的全部内容,希望文章能够帮你解决了解界面布局layout_weight属性所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部