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

 更新时间:2016年04月13日 15:32:36   作者:第二天  
这篇文章主要介绍了Android App中的多个LinearLayout嵌套布局实例,利用线性布局来排列按钮是安卓应用布局中的常用做法,需要的朋友可以参考下

在做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>

相关文章

  • Android开发注解排列组合出启动任务ksp

    Android开发注解排列组合出启动任务ksp

    这篇文章主要为大家介绍了Android开发注解排列组合出启动任务ksp示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • 关于Android SDCard存储的问题

    关于Android SDCard存储的问题

    本篇文章小编为大家介绍,关于Android SDCard存储的问题。需要的朋友参考下
    2013-04-04
  • Android仿微信QQ聊天顶起输入法不顶起标题栏的问题

    Android仿微信QQ聊天顶起输入法不顶起标题栏的问题

    这篇文章主要介绍了Android之仿微信QQ聊天顶起输入法不顶起标题栏问题,本文实例图文相结合给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • Android实现自动变换大小的组件ViewPager2

    Android实现自动变换大小的组件ViewPager2

    这篇文章主要介绍了Android实现自动变换大小的组件ViewPager2,ViewPager2最显著的特点是基于RecyclerView实现,RecyclerView是目前Android端最成熟的AdapterView解决方案
    2023-03-03
  • Android中Fab(FloatingActionButton)实现上下滑动的渐变效果

    Android中Fab(FloatingActionButton)实现上下滑动的渐变效果

    这篇文章主要给大家介绍了Android中FloatingActionButton(简称FAB)是如何实现上下滑动的渐变效果,文中给出了详细的示例代码,相信对大家具有一定的参考价值,有需要的朋友们可以一起看看吧。
    2017-02-02
  • AndroidStudio项目打包成jar的简单方法

    AndroidStudio项目打包成jar的简单方法

    JAR(Java Archive,Java 归档文件)是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件,在eclipse中我们知道如何将一个项目导出为jar包,供其它项目使用呢?下面通过本文给大家介绍ndroidStudio项目打包成jar的简单方法,需要的朋友参考下吧
    2017-11-11
  • Android实现百度地图两点画弧线

    Android实现百度地图两点画弧线

    这篇文章主要为大家详细介绍了Android实现百度地图两点画弧线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android开发之StackView用法和遇到的坑分析

    Android开发之StackView用法和遇到的坑分析

    这篇文章主要介绍了Android开发之StackView用法和遇到的坑,结合实例形式分析了Android StackView图片操作用法及常见问题解决方法,需要的朋友可以参考下
    2019-03-03
  • 图文详解Android Studio搭建Android集成开发环境的过程

    图文详解Android Studio搭建Android集成开发环境的过程

    这篇文章主要以图文的方式详细介绍了Android Studio搭建Android集成开发环境的过程,文中安装步骤介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Android自定义UI手势密码改进版源码下载

    Android自定义UI手势密码改进版源码下载

    这篇文章主要介绍了Android自定义UI手势密码改进版,为大家提供了手势密码源码下载,,具有一定的实用性,感兴趣的小伙伴们可以参考一下
    2016-10-10

最新评论