android:layout_weight属性你真的理解吗
2016-04-16 21:44
363 查看
大家看到这个标题可能会说,你这也太low了吧?权重这个属性谁不知道,谁没用过,有什么好讲的?是的,我确实经常用,但是我们并非真正的理解了它。ok,下面我们说说这一属性。
布局代码:
这是布局图:
首先我们要分三类讲解:
从布局文件大家可以看到我们是按照1:2:3做的权重分配
1. android:layout_width=”0dp”这个就是初学时候我们理解的权重分配
按照1:2:3做的权重分配布局。
2.android:layout_width=”wrap_content”首先系统会给3个view分配它们的宽度值,没错就是wrap_content,然后把剩余的宽度按照1:2:3再分配给他们,即1:2:3
3.android:layout_width=”match_parent”其实前两个很好理解,主要是第三个,同样系统会给3个view分配它们的宽度值就是match_parent,
接下来我们算一算剩余的宽度:
剩余的宽度=屏幕的宽度-3match_parent
你们看是不是这样,嗯,对的,理论上就是这样。那么也可以这样写:
剩余的宽度=match_parent-3match_parent=-2match_parent
so. 剩余的宽度=-2match_parent
好的,大家看下效果图是不是第三行的最后一个TextView不见了?现在我们就可以算下,他为什么消失咯,索性我们算下他们三个的比值。
(这里解释下,下面的计算:首先每个都会现分配一个match_parent,然后在平分剩余宽度,即 match_parent+剩余的宽度*所占的比例)
第一个:
match_parent+(-2)match_parent*1/6=2/3match_parent
第二个:
match_parent+(-2)match_parent*2/6=1/3match_parent
第三个:
match_parent+(-2)match_parent*3/6=0match_parent
好了,这就是我理解的权重,如果有错误,希望大家指出。
权重的平分(按比例划分):
这个确实没有什么好说的。就是我们最初对他的理解。所以这里我们跳过。剩余空间按权重平分(重点):
这就是我想要说的,为了便于我们理解这里我结合布局代码讲解:布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="35dp" android:background="#f00"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="35dp" android:background="#0f0"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:textSize="35dp" android:background="#00f"/> </LinearLayout> <LinearLayout android:layout_marginTop="40dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="35dp" android:background="#f00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="35dp" android:background="#0f0"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" android:textSize="35dp" android:background="#00f"/> </LinearLayout> <LinearLayout android:layout_marginTop="40dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="35dp" android:background="#f00"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="35dp" android:background="#0f0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="3" android:textSize="35dp" android:background="#00f"/> </LinearLayout> </LinearLayout>
这是布局图:
首先我们要分三类讲解:
从布局文件大家可以看到我们是按照1:2:3做的权重分配
1. android:layout_width=”0dp”这个就是初学时候我们理解的权重分配
按照1:2:3做的权重分配布局。
2.android:layout_width=”wrap_content”首先系统会给3个view分配它们的宽度值,没错就是wrap_content,然后把剩余的宽度按照1:2:3再分配给他们,即1:2:3
3.android:layout_width=”match_parent”其实前两个很好理解,主要是第三个,同样系统会给3个view分配它们的宽度值就是match_parent,
接下来我们算一算剩余的宽度:
剩余的宽度=屏幕的宽度-3match_parent
你们看是不是这样,嗯,对的,理论上就是这样。那么也可以这样写:
剩余的宽度=match_parent-3match_parent=-2match_parent
so. 剩余的宽度=-2match_parent
好的,大家看下效果图是不是第三行的最后一个TextView不见了?现在我们就可以算下,他为什么消失咯,索性我们算下他们三个的比值。
(这里解释下,下面的计算:首先每个都会现分配一个match_parent,然后在平分剩余宽度,即 match_parent+剩余的宽度*所占的比例)
第一个:
match_parent+(-2)match_parent*1/6=2/3match_parent
第二个:
match_parent+(-2)match_parent*2/6=1/3match_parent
第三个:
match_parent+(-2)match_parent*3/6=0match_parent
好了,这就是我理解的权重,如果有错误,希望大家指出。
相关文章推荐
- android自定义数据展示view,随手指滑动实现
- Android Studio下jni应用
- VS2015 xamarin android axml 文件添加智能提示
- Android中的Handler和AsyncTask的区别
- Android RGB颜色查询对照表
- 关于Android Studio import时出现cannot find settings.jar错误
- android:visibility中"invisible"与"gone"对android:layout_weight的影响
- android MVC与MVP
- Mac下Android Studio升级SVN1.8(使用1.8format来checkout项目)
- Fragment初探
- Location and Maps(位置和地图)——翻译自developer.android.com
- Android Studio 修改项目包名(任意级)
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
- Android-实现定时任务的几种方法
- Android DrawableUtil 工具类,
- 下载最新Android代码的方法
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
- Android Studio 修改项目包名(最后一级)
- android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1ccfe