android属性之layout_weight深入研究

来源:互联网 发布:c语言培训学校 编辑:程序博客网 时间:2024/05/20 20:18

      在android布局开发中,LinearLayout用的挺多的,而对于LinearLayout中的组件来说,当我想按比例分配他们的布局关系的时候,往往会用到layout_weight,之前也看到过不少关于这样的文章,可是说法不一,于是自己尝试着研究了一下,特把结果共享。

     先来看看google对这个属性的解释:


 官方给的例子就不看了,没多少东西。对于weight官方用了一个词importance,有些人解释为权重,那我也解释成权重,反正对于理解不影响。之前看的不少文章都说什么weight值越低,代表权重越高,所占的比例就越大,但我认为正好相反,直接上例子。

   XML1文件代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView         android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:background="#aa0000"        android:gravity="center"        android:text="1"        android:layout_weight="1"/>    <TextView         android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:background="#aaaa00"        android:gravity="center"        android:text="2"        android:layout_weight="2"/>    <TextView         android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:background="#aaaaaa"        android:gravity="center"        android:text="3"        android:layout_weight="3"/></LinearLayout>


效果图1


没啥说的,直接按照123比例显示,感觉没问题啊,呵呵,再看

xml代码2

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView         android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:background="#aa0000"        android:gravity="center"        android:text="11111111111111111111111"        android:layout_weight="1"/>    <TextView         android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:background="#aaaa00"        android:gravity="center"        android:text="2"        android:layout_weight="2"/>    <TextView         android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:background="#aaaaaa"        android:gravity="center"        android:text="3"        android:layout_weight="3"/></LinearLayout>


效果图2:


这怎么解释,我只是把text的内容加了很长而已。出现这样的原因最后解释,先贴出解决办法,把每个textview的android:layout_width="wrap_content"改为android:layout_width="0dip"就ok了,

直接放效果图3:


是不是中和了图1和图2,标准的按照比例显示,不会因为内容的增加而改变。

如果把android:layout_width="wrap_content"改为android:layout_width="match_parent"呢,接着看

xml代码4

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#aa0000"        android:gravity="center"        android:text="11111111111111111111111"        android:layout_weight="1"/>    <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#aaaa00"        android:gravity="center"        android:text="2"        android:layout_weight="2"/>    <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#aaaaaa"        android:gravity="center"        android:text="3"        android:layout_weight="3"/></LinearLayout>


效果图4


第三个textview没有了,并且也不是按照比例显示了,这是为毛?如果我把weight分别改为678呢

效果图5(weight分别改为678


第三个textview又出来了,但是好像还不是按照比例显示的,这其中到底有什么规律呢?

规律

其实这是一个优先级的问题,就是说layout_width的优先级高,layout_weight的优先级低。先来解释效果图2的情况,当android:layout_width="wrap_content"时,该组件为根据内容的多少而增加该组件的大小。当设置为0dip时,大家都是0,完全不考虑layout_width自适应的影响了,所以会完全按照比例显示,也就是图3的情况。当设置为android:layout_width="match_parent"时,就复杂点,按理说第一个组件应该直接占满真个父组件啊,但是由于weight的影响出现了图4的结果,其实是可以计算出来这样的结果的。

  假设屏幕宽度为all_width,显示三个textview都是match_parent,就出现了3个all_width,这三个组件的宽度就是3个all_width的宽度。隐藏的宽度(即为除去三个组件占去的剩余宽度)(1个all_width)-(3个all_width)=(-2个all_width),到此,按照layout_width分配完毕,由于优先级的关系,下面是layout_weight分配。layout_weight分配的资源只能是layout_widht剩下的,即为(-2个all_width)。textview1所占总空间=layout_width分配的1个all_width+6/(6+7+8)*(-2个all_width)=(3/7个all_width)。经过我精确地测量,图5中第一个组件的确占了3/7。综上所述,就是说一个组件的总显示宽度就是layout_width+layout_weight。下面再通过一个小例子验证总宽度为二者相加

xml代码6

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >        <TextView         android:layout_width="50dp"        android:layout_height="match_parent"        android:background="#aa0000"        android:gravity="center"        android:text="1"        android:layout_weight="1"/>    <TextView         android:layout_width="0dip"        android:layout_height="match_parent"        android:background="#aaaa00"        android:gravity="center"        android:text="2"        android:layout_weight="2"/>    <TextView         android:layout_width="0dip"        android:layout_height="match_parent"        android:background="#aaaaaa"        android:gravity="center"        android:text="3"        android:layout_weight="3"/></LinearLayout>


效果图6


上面的这张图我已经把第一个textview的属性改成android:layout_width="50dp"了,自然看着没有完全按照比例分类,但是第二个组件和第三个组件由于没有增加layout_width,所以它俩的比例一定还是2:3。

现在我把textview1的组件android:layout_width="0dp",并且增加一个android:layout_width="50dp",没有android:layout_weight属性或者说成这样(android:layout_weight="0")背景也为红色的组件,如果效果图中的颜色分布和图6一样就对了,就验证成功了。

xml代码7:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView         android:layout_width="50dp"        android:layout_height="match_parent"        android:background="#aa0000"        android:gravity="center"        android:text="我是增加的"/>    <TextView         android:layout_width="0dip"        android:layout_height="match_parent"        android:background="#aa0000"        android:gravity="center"        android:text="1"        android:layout_weight="1"/>    <TextView         android:layout_width="0dip"        android:layout_height="match_parent"        android:background="#aaaa00"        android:gravity="center"        android:text="2"        android:layout_weight="2"/>    <TextView         android:layout_width="0dip"        android:layout_height="match_parent"        android:background="#aaaaaa"        android:gravity="center"        android:text="3"        android:layout_weight="3"/></LinearLayout>


效果图7:


事实证明,颜色分布一样的,验证成功。



0 0
原创粉丝点击