Android App中的多个LinearLayout嵌套布局实例解析

所属分类: 软件编程 / Android 阅读数: 1803
收藏 0 赞 0 分享

在做android  UI布局时,用了LinearLayout嵌套,发现效果并不如我预料一般
查了下资料,说是要设置layout_weight属性
资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。
于是静下心来,自己写xml测试,发现如下。
如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的,
换句话说最外层不能用layout_weight
xml布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
> 
 <LinearLayout 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:orientation="vertical"> 
 <TextView 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="zzzzzzzzzzzzzzzzzzzzzzzzzzzzz" 
  android:textSize="18sp" 
  android:layout_marginLeft="5px" 
  android:layout_marginBottom="10px" 
  android:textColor="@android:color/darker_gray" 
  /> 
  
 <TextView android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="xxxxxxxxxxxxxxxxxxxxxxx" 
  android:layout_marginLeft="50px" 
  android:layout_marginBottom="10px" 
   android:textSize="18sp" 
/> 
 </LinearLayout> 
  
</LinearLayout> 

这个能正常显示,但当我们把嵌套的LinearLayout方向设置成水平,第一个TextView充满整个LinearLayout,第二个TextView控件不显示。
当我们为两个TextView加上 android:layout_weight="1"属性时,能显示,效果我就不说了,大家都懂的。
发现一个有趣的现象:我们将 两个控件的android:layout_weight="1"删掉,嵌套的LinearLayout方向属性删掉,代码和最开始一样
注意,我们前面说上面的xml它能正常显示,现在,一模一样的xml代码则显示错误。
当我们只设置一个控件的android:layout_weight="1"属性时,发现也会有显示错误
ps:我只是用可视化工具看了一下 ,并未编译,说出来,只是告诉大家不要被它的可视化效果误导(目测是工具的原因)。至于编译后会如何显示,这个有兴趣的可以去看下。我说的显示错误并不是说文件有错误,只是在说没有达到我想要的效果(都显示出来)。


更进一步,来看这样一个效果:

2016413153104827.png (322×458)

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <View
  android:layout_width="fill_parent"
  android:layout_height="2dip"
  android:background="#E4E4E4" />

 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <LinearLayout
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/icon_main_sugar" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="水"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>

  <View
   android:layout_width="2dip"
   android:layout_height="match_parent"
   android:background="#E4E4E4" />

  <LinearLayout
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/icon_main_eat" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="餐具"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>

  <View
   android:layout_width="2dip"
   android:layout_height="match_parent"
   android:background="#E4E4E4" />

  <LinearLayout
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/icon_main_info" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="信息"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>
 </LinearLayout>

 <View
  android:layout_width="fill_parent"
  android:layout_height="2dip"
  android:background="#E4E4E4" />

 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/evaluate_self" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="糖类"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>

  <View
   android:layout_width="2dip"
   android:layout_height="match_parent"
   android:background="#E4E4E4" />

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/target_manager" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="糖类"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>

  <View
   android:layout_width="2dip"
   android:layout_height="match_parent"
   android:background="#E4E4E4" />

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/drug_manager" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="糖类"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>
 </LinearLayout>

 <View
  android:layout_width="fill_parent"
  android:layout_height="2dip"
  android:background="#E4E4E4" />

 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/news_share" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="新聞"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>

  <View
   android:layout_width="2dip"
   android:layout_height="match_parent"
   android:background="#E4E4E4" />

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/set_manager" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="設置"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>

  <View
   android:layout_width="2dip"
   android:layout_height="match_parent"
   android:background="#E4E4E4" />

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:orientation="vertical" >

   <ImageView
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:src="@drawable/content_report" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="3dip"
    android:text="任務"
    android:textColor="#7C8187"
    android:textSize="15dip" />
  </LinearLayout>
 </LinearLayout>
 <View
  android:layout_width="fill_parent"
  android:layout_height="2dip"
  android:background="#E4E4E4" />

</LinearLayout>

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多