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

在做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:我只是用可视化工具看了一下 ,并未编译,说出来,只是告诉大家不要被它的可视化效果误导(目测是工具的原因)。至于编译后会如何显示,这个有兴趣的可以去看下。我说的显示错误并不是说文件有错误,只是在说没有达到我想要的效果(都显示出来)。

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

代码如下:

<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
, 布局
linearlayout
linearlayout嵌套、linearlayout嵌套层数、linearlayout垂直布局、linearlayout横向布局、linearlayout布局属性,以便于您获取更多的相关知识。

时间: 2024-09-08 04:42:57

Android App中的多个LinearLayout嵌套布局实例解析_Android的相关文章

Android App中各种数据保存方式的使用实例总结_Android

少量数据保存之SharedPreferences接口实例SharedPreferences数据保存主要是通过键值的方式存储在xml文件中 xml文件在data/此程序的包名/XX.xml. 格式: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <int name="count" value="3" /> <string name="ti

Android App中实现图片异步加载的实例分享_Android

一.概述一般大量图片的加载,比如GridView实现手机的相册功能,一般会用到LruCache,线程池,任务队列等:那么异步消息处理可以用哪呢? 1.用于UI线程当Bitmap加载完成后更新ImageView 2.在图片加载类初始化时,我们会在一个子线程中维护一个Loop实例,当然子线程中也就有了MessageQueue,Looper会一直在那loop停着等待消息的到达,当有消息到达时,从任务队列按照队列调度的方式(FIFO,LIFO等),取出一个任务放入线程池中进行处理. 简易的一个流程:当需

Android App中实现相册瀑布流展示的实例分享_Android

传统界面的布局方式总是行列分明.坐落有序的,这种布局已是司空见惯,在不知不觉中大家都已经对它产生了审美疲劳.这个时候瀑布流布局的出现,就给人带来了耳目一新的感觉,这种布局虽然看上去貌似毫无规律,但是却有一种说不上来的美感,以至于涌现出了大批的网站和应用纷纷使用这种新颖的布局来设计界面. 记得我在之前已经写过一篇关于如何在Android上实现照片墙功能的文章了,但那个时候是使用的GridView来进行布局的,这种布局方式只适用于"墙"上的每张图片大小都相同的情况,如果图片的大小参差不齐,

Android App中使用ViewPager实现滑动分页的要点解析_Android

以前如果要做 Tab 分页的话,必须要用一个很难用的 TabActivity,而且做出来的效果很差,弹性也很小 忘了从什么时候开始,Google release 了 ViewPager 这好东西取代了以前难用的 Gallery 元件,加上从 Honeycomb 导入的 Fragment 之后终于能够简单做出好看又好用的 Layout 了! 这里我们采用PagerTabStrip ,做出来的效果如下 特色就是使用简单,出来的效果则是目前显示的分页 Tab 的文字会自动置中,然后分别在左右显示上一个

Android App中使用AudioManager类来编写音频播放器_Android

手机都有声音模式,声音.静音还有震动,甚至震动加声音兼备,这些都是手机的基本功能.在Android手机中,我们同样可以通过Android的SDK提供的声音管理接口来管理手机声音模式以及调整声音大小,这就是Android中AudioManager的使用. AudioManager 类位于 android.Media 包中,该类提供访问控制音量和钤声模式的操作   以下分别是AudioManager设置声音模式和调整声音大小的方法.    如何获取声音管理器: AudioManager audioM

Android App中DrawerLayout抽屉效果的菜单编写实例_php技巧

抽屉效果的导航菜单看了很多应用,觉得这种侧滑的抽屉效果的菜单很好. 不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而且感觉能放很多东西. 库的引用: 首先, DrawerLayout这个类是在Support Library里的,需要加上android-support-v4.jar这个包. 然后程序中用时在前面导入import android.support.v4.widget.DrawerLayout; 如果找不到这个类,首先用SDK Manager更新

Android App中使用RatingBar实现星级打分功能的教程_Android

RatingBar简单介绍RatingBar是基于SeekBar(拖动条)和ProgressBar(状态条)的扩展,用星形来显示等级评定,在使用默认RatingBar时,用户可以通过触摸/拖动/按键(比如遥控器)来设置评分, RatingBar自带有两种模式 ,一个小风格 ratingBarStyleSmall,大风格为ratingBarStyleIndicator,大的只适合做指示,不适用与用户交互. 自定义RatingBar需要注意的地方 一般情况下,系统自带的RatingBar是远远无法满

Android编程中常用适配器及自定义适配器用法实例分析_Android

本文实例讲述了Android编程中常用适配器及自定义适配器用法.分享给大家供大家参考,具体如下: 一.适配器. 顾名思义,就是把一些数据给弄得适当,适合以便于在View上显示.可以看作是界面数据绑定的一种理解.它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等.适配器就像显示器,把复杂的东西按人可以接受的方式来展现. 那么适配器是怎么处理得到的数据,并把它显示出来的呢.其实很简单,说白了适配器它也是一个类,在类里面它实现了父类的这几个方法: publicint getCoun

Android App中实现相册瀑布流展示的实例分享

传统界面的布局方式总是行列分明.坐落有序的,这种布局已是司空见惯,在不知不觉中大家都已经对它产生了审美疲劳.这个时候瀑布流布局的出现,就给人带来了耳目一新的感觉,这种布局虽然看上去貌似毫无规律,但是却有一种说不上来的美感,以至于涌现出了大批的网站和应用纷纷使用这种新颖的布局来设计界面. 记得我在之前已经写过一篇关于如何在Android上实现照片墙功能的文章了,但那个时候是使用的GridView来进行布局的,这种布局方式只适用于"墙"上的每张图片大小都相同的情况,如果图片的大小参差不齐,