android LinearLayout佈局嵌套覆蓋問題

 

android LinearLayout佈局嵌套覆蓋問題

在做android  UI佈局時,用了LinearLayout嵌套,發現效果並不如我預料一般

查了下資料,說是要設置layout_weight屬性

資料說得不是很清楚,也沒仔細看,就去弄,結果越弄越混亂。

於是靜下心來,自己寫xml測試,發現如下。

我用eclipse開發,用android Common XML Editor   使用快捷鍵alt+/

一、如果LinearLayout是最外面的一層,它是不會彈出layout_weight屬性的

換句話說最外層不能用layout_weight

二、xml佈局如下

[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4. >  
  5.   <LinearLayout  
  6.       android:layout_width="fill_parent"  
  7.       android:layout_height="wrap_content"  
  8.       android:orientation="vertical">  
  9.     <TextView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="zzzzzzzzzzzzzzzzzzzzzzzzzzzzz"   
  13.         android:textSize="18sp"  
  14.         android:layout_marginLeft="5px"  
  15.         android:layout_marginBottom="10px"  
  16.         android:textColor="@android:color/darker_gray"  
  17.        />  
  18.       
  19.     <TextView  android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="xxxxxxxxxxxxxxxxxxxxxxx"  
  22.         android:layout_marginLeft="50px"  
  23.         android:layout_marginBottom="10px"  
  24.          android:textSize="18sp"  
  25. />  
  26.   </LinearLayout>  
  27.      
  28. </LinearLayout>  


這個能正常顯示,但當我們把嵌套的LinearLayout方向設置成水平,第一個TextView充滿整個LinearLayout,第二個TextView控件不顯示。

當我們爲兩個TextView加上 android:layout_weight="1"屬性時,能顯示,效果我就不說了,大家都懂的。

發現一個有趣的現象:我們將 兩個控件的android:layout_weight="1"刪掉,嵌套的LinearLayout方向屬性刪掉,代碼和最開始一樣

注意,我們前面說上面的xml它能正常顯示,現在,一模一樣的xml代碼則顯示錯誤。

當我們只設置一個控件的android:layout_weight="1"屬性時,發現也會有顯示錯誤

ps:我只是用可視化工具看了一下 ,並未編譯,說出來,只是告訴大家不要被它的可視化效果誤導(目測是工具的原因)。至於編譯後會如何顯示,這個有興趣的可以去看下。我說的顯示錯誤並不是說文件有錯誤,只是在說沒有達到我想要的效果(都顯示出來)。

二、上面中只是一個LinearLayout嵌套一個LinearLayout,如果是嵌套兩個LinearLayout呢,那麼我們不僅要設置每個Linearlayout的裏面控件的權(layout_weight)屬性,還要設置嵌套的LinearLayout的權屬性

 三、有了上面的知識,我於是將我項目的佈局弄出來,結果心中信念瞬間崩塌,上段代碼:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.       
  8.    <LinearLayout android:layout_width="fill_parent"  
  9.        android:layout_height="wrap_content"  
  10.        android:orientation="horizontal"  
  11.        android:layout_marginLeft="50px"  
  12.        android:layout_marginRight="50px"  
  13.        android:layout_marginBottom="15px">  
  14.     <TextView  android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="姓名"  
  17.         android:layout_weight="5"  
  18.          android:textSize="18sp"/>  
  19.     <EditText android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/name"  
  22.         android:hint="請輸入您的姓名"  
  23.         android:layout_weight="1"/>  
  24. </LinearLayout>    
  25.   
  26.    <LinearLayout android:layout_width="fill_parent"  
  27.        android:layout_height="wrap_content"  
  28.        android:orientation="horizontal"  
  29.        android:layout_marginLeft="50px"  
  30.        android:layout_marginRight="50px"  
  31.        android:layout_marginBottom="15px">     
  32.     <TextView  android:layout_width="fill_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="年齡"  
  35.         android:layout_weight="5"  
  36.         android:textSize="18sp"/>  
  37.       
  38.     <EditText android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:id="@+id/age"  
  41.         android:hint="請輸入您的年齡"  
  42.         android:layout_weight="1"/>  
  43.  </LinearLayout>    
  44.    
  45. </LinearLayout>  


效果圖:

這完全顛覆了我第二點的結論,淚奔.......我發誓,第二個結論我也做了好幾次測試才得出的結論

 

總結,如果佈局中用到LinearLayout嵌套,那麼你注意設置它的layout_weight  可視化工具有點扯蛋   慢慢調試吧

另附我的項目佈局的一點經驗

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.    <LinearLayout android:layout_width="fill_parent"  
  8.        android:layout_height="wrap_content"  
  9.        android:orientation="horizontal"  
  10.        android:layout_marginLeft="50px"  
  11.        android:layout_marginRight="50px"  
  12.        android:layout_marginBottom="15px"  
  13.        android:layout_gravity="center"  
  14.        android:gravity="center">    
  15.         <TextView    
  16.            android:layout_width="fill_parent"  
  17.            android:layout_height="wrap_content"  
  18.            android:text="性別"  
  19.            android:textSize="18sp"  
  20.            android:layout_weight="3"/>         
  21.          <RadioGroup android:id="@+id/radioGroup"   
  22.              android:contentDescription="性別"   
  23.              android:layout_width="fill_parent"   
  24.              android:layout_height="wrap_content"  
  25.              android:orientation="horizontal"  
  26.              android:layout_weight="1">  
  27.                    
  28.             <RadioButton android:layout_width="wrap_content"   
  29.                 android:layout_height="wrap_content"   
  30.                 android:id="@+id/radioMale"   
  31.                 android:text="男"   
  32.                 android:checked="true"  
  33.                 android:layout_marginRight="15px"  
  34.                 android:textSize="18sp">  
  35.                 </RadioButton>  
  36.             <RadioButton android:layout_width="wrap_content"   
  37.                 android:layout_height="wrap_content"   
  38.                 android:id="@+id/radioFemale"   
  39.                 android:text="女"  
  40.                 android:textSize="18sp">  
  41.                 </RadioButton>  
  42.           </RadioGroup>  
  43.  </LinearLayout>  
  44.       
  45. </LinearLayout>  


 開始時RadioGroup的layout_width="wrap_content",怎麼設置權都達不到想要的效果。要改成fill_parent

RadioButton的尺寸比TextView大  所以顯示時TextView在上方,設置LinearLayout中android:gravity="center">即可


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章