前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Android]对于Android:Layout_weight的深刻理解

[Android]对于Android:Layout_weight的深刻理解

作者头像
祥知道
发布2020-03-10 14:45:31
5960
发布2020-03-10 14:45:31
举报
文章被收录于专栏:祥的专栏祥的专栏

看到一篇讲Android:Layout_weight不错的文章,所以就分享出来给大家,对原文进行了一些排版。

首先看一下Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重。很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间。 看下面代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>     
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    android:orientation="vertical"     
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent" >     
	<EditText     
		android:layout_width="fill_parent"     
		android:layout_height="wrap_content"     
		android:gravity="left"     
		android:text="one"/>     
	<EditText     
		android:layout_width="fill_parent"     
		android:layout_height="wrap_content"     
		android:gravity="center"     
		android:layout_weight="1.0"     
		android:text="two"/>     
	 <EditText     
		android:layout_width="fill_parent"     
		android:layout_height="wrap_content"     
		android:gravity="right"     
		android:text="three"/>     
</LinearLayout>

得到的运行效果是这样的:

看上面代码:只有EditView2使用了Layout_weight属性,并赋值为了1,而EditView1和EditView3没有设置Layout_weight这个属性,根据API,可知,他们默认是0下面我就来讲,Layout_weight这个属性的真正的意思:Android系统先按照你设置的3个EditView高度Layout_height值wrap_content,给你分配好他们3个的高度,然后会把剩下来的屏幕空间全部赋给EditView2,因为只有他的权重值是1,这也是为什么EditView2占了那么大的一块空间。 有了以上的理解我们就可以对网上关于Layout_weight这个属性更让人费解的效果有一个清晰的认识了。 我们来看这段代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >   
    <TextView   
        android:background="#ff0000"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="1"   
        android:textColor="@android:color/white"   
        android:layout_weight="1"/>   
    <TextView   
        android:background="#cccccc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="2"   
        android:textColor="@android:color/black"   
        android:layout_weight="2" />   
     <TextView   
        android:background="#ddaacc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="3"   
        android:textColor="@android:color/black"   
        android:layout_weight="3" />   
</LinearLayout> 

三个文本框的都是 layout_width=“wrap_content”时,会得到以下效果

按照上面的理解,系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容1,2,3即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,所以就出现了上面的图像。 而当layout_width=“fill_parent”时,如果分别给三个TextView设置他们的Layout_weight为1、2、2的话,就会出现下面的效果:

你会发现1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=“fill_parent”时,weighth值越小权重越大,优先级越高,就好像在背口诀一样,其实他们并没有真正理解这个问题,真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我们来分析: 系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度 那么这时候

剩余空间 = 1*parent_width - 3*parent_width = -2*parent_width

(其中parent_width指的是屏幕宽度 )

那么第一个TextView的实际所占宽度应该 = fill_parent的宽度,即

parent_width+ 他所占剩余空间的权重比列1/5 * 剩余空间大小(-2 parent_width)= 3/5*parent_width

同理第二个TextView的实际所占宽度= parent_width + 2/5*(-2parent_width) = 1/5*parent_width; 第三个TextView的实际所占宽度 = parent_width + 2/5*(-2parent_width)= 1/5*parent_width;所以就是3:1:1的比列显示了。 这样你也就会明白为什么当你把三个Layout_weight设置为1、2、3的话,会出现下面的效果了:

第三个直接不显示了,为什么呢?一起来按上面方法算一下吧: 系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度 那么这时候的剩余空间 = 1*parent_width - 3*parent_width = -2*parent_width (parent_width指的是屏幕宽度 ) 那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即

parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)= 2/3*parent_width

同理第二个TextView的实际所占宽度 = parent_width + 2/6*(-2*parent_width)=1/3*parent_width; 第三个TextView的实际所占宽度 = parent_width + 3/6*(-2*parent_width) =0*parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。

原文连接:http://blog.csdn.net/xiechengfa/article/details/38334327

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档