包含在LinearLayout布局内部的控件和布局可以设置weight属性,用来设置控件和子布局在某个方向上占LinearLayout的比例。
影响weight属性效果的几个属性
1、LinearLayout的orientation值
当LinearLayout的android:orientation="horizontal"时,可以对其内部的控件和布局在水平方向上所占比例进行设置;
当LinearLayout的android:orientation="vertical"时,可以对其内部的控件和布局在垂直方向上所占比例进行设置;
2、内部控件或布局的height属性或width属性
(1)当LinearLayout的android:orientation="horizontal"时,其内部的控件和布局的width属性对weight有影响。
此时分两种情况:
① android:layout_width="0dp"
此时设置控件或布局的weight值为几,就直接表示该控件或布局占总份数中的几份。
如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#ADFF2F"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#DA70D6"
android:layout_weight="2"/>
</LinearLayout>
LinearLayout内部的第一个LinearLayout占比例为1/3,第二个LinearLayout占的比例为2/3。
如图所示:
② android:layout_width= "wrap_content"
实际上是直接就按比例划分的,如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
表示三个TextView水平方向依次排列,第一个TextView宽度占1/6,第二个TextView宽度占2/6,第三个TextView宽度占3/6。
实现效果图如下:
③ android:layout_width= "match_parent" 或 android:layout_width="fill_parent"
此时需要计算
如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
显示效果如下图
为什么不是按1:2:3的比例进行划分的呢?
因为当width值为match_parent(fill_parent)时,计算方法发生变化,具体如下:
第一步:LinearLayout中有几个需要按权重分配宽度的控件或布局,上面的代码中LinearLayout中有3个TextView,由于每一个TextView的width值都是fill_parent,但是由于只有一个屏幕,1 - 3 = -2。的
第二步:三个TextView所占比例依次为1/6 ,2/6,3/6。
第三步:按照先到先得的原则,第一个TextView实际分配比例算法为 1 - 2 * ( 1/6 ) = 2/3,
第二个TextView分配比例为 1 - 2 * ( 2/6 ) = 1/3,
第三个TextView分配比例为 1 - 2 * ( 3/6 ) = 0。
即三个TextView分配比例依次为2 : 1 : 0。
如果将三个TextView的weight依次设置为2、3、4呢?
根据上面的计算规则,第一个TextView实际分配的比例为1 - 2 * ( 2/9 ) = 5/9,
第二个TextView实际分配的比例为1 - 2 * ( 3/9 ) = 3/9,
第三个TextView实际分配的比例为1 - 2 * ( 4/9 ) = 1/9。
即三个TextView分配比例依次为 5 : 3 : 1
效果如下图所示:
(2)height属性同理
参考文章:http://www.runoob.com/w3cnote/android-tutorial-linearlayout.html