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">即可


更多相关文章

  1. Android解决ViewPager嵌套ViewPager中多层Fragment子ViewPager中
  2. Android(安卓)清单目录application对应属性讲解
  3. android studio gradle 多版本多apk打包(打包系列教程之五)
  4. 修正AppCompatSpinner弹出框位置问题
  5. Android(安卓)Intent 对象详解
  6. Android桌面组件App Widget开发三步走
  7. Android学习整理 -1- Fragment 学习
  8. Android(安卓)实现自定义的卫星式菜单(弧形菜单)View
  9. Android语录

随机推荐

  1. android studio 降低项目版本错误,no reso
  2. Android(安卓)修改默认USB模式
  3. 【Android】UI-shape的使用
  4. Android修行之路——Dialog
  5. 国内外Android知名技术博客汇总
  6. Android(安卓)Apk反编译、签名
  7. Android:开发环境搭建
  8. Android写文件到SDCard的一般过程和代码
  9. google map my demo
  10. Android进程保活