以前一直没弄懂Layout_weight是什么意思,自己写代码测试也出来了不同的情况,最近看了一篇帖子感觉分析的很好,转贴出来学习下。
布局文件是:
复制代码
出现的布局是:button1占了2/3,button2占了1/3。

但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。

出现这样的结局是什么意思呢?下面是人家的详解:转载过来:
*******转载的解释*********
linearLayout中包含有weight的child时,linearLayout会measure两次:
设屏幕宽度为X
第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X
第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为
x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x
那我现在对这句话重新概括一下:“因为设置了button1的权重最小,所以它占用的布局优先级就越高”,也许在Android里面布局并没有优先级之说,我这里只是为了说明问题,自己定义的,所以朋友们不要拍砖。
那首先分析一下當layout_width屬性設置為fill_parent的時候,即充滿父佈局,當然意思是這個控件要根據weight的設置盡可能的大,因此,依上例而論,button1的weight設為1,button2的weight設置為2.即button的優先級最高,因此,要填充父佈局就要button1先來填充,盡可能的大,那這個盡可能又是多少呢,這就要綜合layout裡其他控件的weight值了,然後做一下運算,button1佔據2/3,button2佔據1/3.你也可以把button2設置為一個非常大的數,比如2000,此時在Graphical Layout模式下可以看到button1填充滿了整個寬度,而看不到button2的影子,事實上button2還是存在的,你把鼠標指向button1的後面就可以看到一個長長的豎條,那個就是button2,已經非常非常小了。因此,在layout_width設置為fill_parent的時候,weight所代表的是你的控件要優先盡可能的大。
接著是當layout_weight設置為wrap_content的時候,即適應內容的寬度,意思是這個控件要盡可能的小,只要能把內容顯示出來就可以了,同樣的,如果把button1和button2的layout_weight設置為wrap_content後,button1的weight為1,button2的weight為2.那麼button1要優先盡可能的小,而button2也要盡可能的小,只是優先級不一樣,因為設置了weight,所以這兩個控件總的寬度要填滿父佈局的寬度,所以就又要計算每個控件所佔據的大小,此時,button1的優先級較高,共有兩份,一份1/3,一份2/3,button1要盡可能的小,那button1當然要選1/3,因此,我們看到的效果反而是button2佔據的較大。這裡要說的是如果把權值同樣做如下設置:button1為1,button2為2000,那button1是不是就要佔據1/2000的空間呢?這麼理解就錯了,剛才說了,要盡可能的小,但這個小是有一個限度的,那就是wrap_content,就是還要是內容完完整整的顯示出來,同樣的,盡可能的大也是有一個限度的,那就是父佈局的寬度。因此,在layout_width設置為wrap_content的時候,weight所代表的是你的控件要優先盡可能的大。
所以,要對weight做了解,要深深的理解下面兩句話:
在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即fill_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.
layout_height 同 layout_width.
布局文件是:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Button1"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:text="Button2"
- />
- </LinearLayout>

但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。

出现这样的结局是什么意思呢?下面是人家的详解:转载过来:
*******转载的解释*********
linearLayout中包含有weight的child时,linearLayout会measure两次:
设屏幕宽度为X
第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X
第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为
x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x
那我现在对这句话重新概括一下:“因为设置了button1的权重最小,所以它占用的布局优先级就越高”,也许在Android里面布局并没有优先级之说,我这里只是为了说明问题,自己定义的,所以朋友们不要拍砖。
那首先分析一下當layout_width屬性設置為fill_parent的時候,即充滿父佈局,當然意思是這個控件要根據weight的設置盡可能的大,因此,依上例而論,button1的weight設為1,button2的weight設置為2.即button的優先級最高,因此,要填充父佈局就要button1先來填充,盡可能的大,那這個盡可能又是多少呢,這就要綜合layout裡其他控件的weight值了,然後做一下運算,button1佔據2/3,button2佔據1/3.你也可以把button2設置為一個非常大的數,比如2000,此時在Graphical Layout模式下可以看到button1填充滿了整個寬度,而看不到button2的影子,事實上button2還是存在的,你把鼠標指向button1的後面就可以看到一個長長的豎條,那個就是button2,已經非常非常小了。因此,在layout_width設置為fill_parent的時候,weight所代表的是你的控件要優先盡可能的大。
接著是當layout_weight設置為wrap_content的時候,即適應內容的寬度,意思是這個控件要盡可能的小,只要能把內容顯示出來就可以了,同樣的,如果把button1和button2的layout_weight設置為wrap_content後,button1的weight為1,button2的weight為2.那麼button1要優先盡可能的小,而button2也要盡可能的小,只是優先級不一樣,因為設置了weight,所以這兩個控件總的寬度要填滿父佈局的寬度,所以就又要計算每個控件所佔據的大小,此時,button1的優先級較高,共有兩份,一份1/3,一份2/3,button1要盡可能的小,那button1當然要選1/3,因此,我們看到的效果反而是button2佔據的較大。這裡要說的是如果把權值同樣做如下設置:button1為1,button2為2000,那button1是不是就要佔據1/2000的空間呢?這麼理解就錯了,剛才說了,要盡可能的小,但這個小是有一個限度的,那就是wrap_content,就是還要是內容完完整整的顯示出來,同樣的,盡可能的大也是有一個限度的,那就是父佈局的寬度。因此,在layout_width設置為wrap_content的時候,weight所代表的是你的控件要優先盡可能的大。
所以,要對weight做了解,要深深的理解下面兩句話:
在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即fill_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.
layout_height 同 layout_width.
主::类中设置权重方法:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1));
-
布局权重
线性布局支持给个别的子视图设定权重,通过android:layout_weight属性。就一个视图在屏幕上占多大的空间而言,这个属性给其设定了一个重要的值。一个大的权重值,允许它扩大到填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组剩余的其他的空间将会分配给其声明权重的子视图。默认的权重是0;
未使用权重前效果图:

俩个线性布局组件,代码如下:
01.<?xmlversion="1.0"encoding="utf-8"?>02.<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"03.android:layout_width="match_parent"04.android:layout_height="match_parent"05.android:orientation="vertical">06.<ScrollView07.android:id="@+id/scrollView_content"08.android:layout_width="match_parent"09.android:layout_height="wrap_content"10.android:layout_gravity="center_horizontal"11.12.android:orientation="vertical">13.<LinearLayout14.android:layout_width="wrap_content"15.android:layout_height="wrap_content"16.android:layout_gravity="center_horizontal"17.android:orientation="vertical">18.<ImageView19.android:layout_width="wrap_content"20.android:layout_height="wrap_content"21.android:layout_gravity="center_horizontal"22.android:src="@drawable/message_selected"/>23.<TextView24.android:layout_width="wrap_content"25.android:layout_height="wrap_content"26.android:layout_gravity="center_horizontal"27.android:padding="10dp"28.android:text="测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n29."30.android:textSize="20sp"/>31.</LinearLayout>32.</ScrollView>33.<LinearLayout34.android:layout_width="wrap_content"35.android:layout_height="wrap_content"36.android:layout_gravity="center_horizontal"37.android:orientation="horizontal">38.<Button39.android:id="@+id/btn1"40.android:layout_width="wrap_content"41.android:layout_height="wrap_content"42.android:text="嵌套Fragment"/>43.<Button44.android:id="@+id/btn2"45.android:layout_width="wrap_content"46.android:layout_height="wrap_content"47.android:text="外部Fragment"/>48.</LinearLayout>49.</LinearLayout>仔细看下和想下就会发现这有个严重的问题,那就是“测试文字”行数多有,第二个LinearLayout布局被挤压或挤出显示区,如下图:

解决方法如下,在第一个LinearLayout 中加入权重android:layout_weight="1",代码如下:
01.<?xmlversion="1.0"encoding="utf-8"?>02.<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"03.android:layout_width="match_parent"04.android:layout_height="match_parent"05.android:orientation="vertical">06.<ScrollView07.android:id="@+id/scrollView_content"08.android:layout_width="match_parent"09.android:layout_height="wrap_content"10.android:layout_gravity="center_horizontal"11.android:layout_weight="1"12.android:orientation="vertical">13.<LinearLayout14.android:layout_width="wrap_content"15.android:layout_height="wrap_content"16.android:layout_gravity="center_horizontal"17.android:orientation="vertical">18.<ImageView19.android:layout_width="wrap_content"20.android:layout_height="wrap_content"21.android:layout_gravity="center_horizontal"22.android:src="@drawable/message_selected"/>23.<TextView24.android:layout_width="wrap_content"25.android:layout_height="wrap_content"26.android:layout_gravity="center_horizontal"27.android:padding="10dp"28.android:text="测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n29."30.android:textSize="20sp"/>31.</LinearLayout>32.</ScrollView>33.<LinearLayout34.android:layout_width="wrap_content"35.android:layout_height="wrap_content"36.android:layout_gravity="center_horizontal"37.android:orientation="horizontal">38.<Button39.android:id="@+id/btn1"40.android:layout_width="wrap_content"41.android:layout_height="wrap_content"42.android:text="嵌套Fragment"/>43.<Button44.android:id="@+id/btn2"45.android:layout_width="wrap_content"46.android:layout_height="wrap_content"47.android:text="外部Fragment"/>48.</LinearLayout>49.</LinearLayout>效果如下图

这样就算文字内容再长也不会把第二个LinearLayout 挤出显示区或挤压,权重布局原理图

本文详细解析了Android中布局权重(layout_weight)的工作原理,特别是在不同layout_width设置下的行为差异,并通过实例展示了如何利用权重解决布局问题。
282

被折叠的 条评论
为什么被折叠?



