有关layout_weight详解。

有关的layout_weight代码:

 

 

<?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:text="one"/>      <EditText          android:layout_width="fill_parent"          android:layout_height="wrap_content"                  android:layout_weight="1.0"          android:text="two"/>      <EditText          android:layout_width="fill_parent"          android:layout_height="wrap_content"             android:text="three"/>      </LinearLayout>  

 

分析:layout_weight属性,Android系统先按照你设置的3个EditText高度Layout_height值wrap_content,给你分配好他们3个的高度,然后会把剩下来的屏幕空间全部赋给EditText2,因为只有他的权重值是1,这也是为什么EditText2占了那么大的一块空间

 

将上面的代码copy到 layout.xml 就能得到如下图效果:

有关layout_weight详解。

有了以上观点,我们再来看看以下代码:

<?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"     
    >     
<EditText     
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"   
    android:background="#ff0000"  
    android:layout_weight="1.0"
    android:text="one"/>     
<EditText     
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"     
     android:background="#dfdfdf"    
    android:layout_weight="2.0"     
    android:text="two"/>     
<EditText     
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"     
    android:background="#ddaacc"
    android:layout_weight="3.0" 
    android:text="three"/>     
</LinearLayout>

分析:当“orientation=horizontal"根据edittext 控件的layou_width,和 layout_weight 属性。很明显是将剩下的空间按1:2:3分别分配给EditText控件。

效果图如下 :

而当layout_width=“fill_parent时,如果分别给三个TextView设置他们的Layout_weight122的话,就会出现下面的效果:3:1:1


分析: 系统先按照layout_width="fill_parent"给3个EditText  分配 "parentWidth"大小的空间(真空屏幕宽度)。那么 这里的剩下的空间是多少??????     “剩下空间”=  “1 *parentWidth”- "3*parentWidth"= -2*parentWidth.   那么 “one真正的空间”= 1*parentWidth-(1/5)*2*parentWidth=3/5*parentWidth..同理我们可以得到two,three 的都为真正空间为1/5*parentWidth...所以可以效果图为3:1:1.。


 

 

 

 

 

你可能感兴趣的:(有关layout_weight详解。)