深入LinearLayout

1. LinearLayout布局的嵌套

2. 奇葩的 layout_weight 属性

 

布局是一种不可见的控件

 

    代码如下

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <LinearLayout
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_margin="20dp"   //间隔
13         android:orientation="vertical"   
14         android:background="#880000">
15         <TextView
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:textSize="20sp"    //文字大小
19             android:text="first"/>
20         <TextView
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:textSize="20sp"
24             android:text="second"/>
25     </LinearLayout>
26     
27     <LinearLayout
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:layout_margin="20dp"
31         android:orientation="vertical"   
32         android:background="#880000">
33         <TextView
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:textSize="20sp"
37             android:text="third"/>
38         <TextView
39             android:layout_width="wrap_content"
40             android:layout_height="wrap_content"
41             android:textSize="20sp"
42             android:text="fourth"/>
43     </LinearLayout>
44     
45 </LinearLayout>

          效果如下

 

 

2. 奇葩的 layout_weight 属性

      使用条件:

   

       

    代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <TextView
10             android:layout_width="wrap_content"
11             android:layout_height="wrap_content"
12             android:background="#00FF00"
13             android:textSize="20sp"
14             android:text="first"/>
15     <TextView
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:background="#0000FF"
19             android:textSize="20sp"
20             android:text="secondddddd"/>    
21             
22 </LinearLayout>

     

     加入Layout_weight属性之后

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <TextView
10             android:layout_width="wrap_content"
11             android:layout_height="wrap_content"
12             android:background="#00FF00"
13             android:textSize="20sp"
14             android:layout_weight="1"   //加入weight属性
15             android:text="first"/>
16     <TextView
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:background="#0000FF"
20             android:textSize="20sp"
21             android:layout_weight="1"
22             android:text="secondddddd"/>    
23             
24 </LinearLayout>

         两个红色的箭头长短平均分配的!!!!

         

      但是这不能保证两个控件大小一样,因此将本身控件width设置为0dp就好了!!!

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <TextView
10         android:layout_width="0dp"
11         android:layout_height="wrap_content"
12         android:background="#00FF00"
13         android:layout_weight="1"
14         android:text="First"/>
15     <TextView
16         android:layout_width="0dp"
17         android:layout_height="wrap_content"
18         android:background="#0000FF"
19         android:layout_weight="1"
20         android:text="Second"/>
21     
22 </LinearLayout>

     

posted @ 2014-06-26 10:44  Mirrorhanman  阅读(193)  评论(0编辑  收藏  举报