这几天一直在研究线性布局这块,记录下一些研究心得,作为参考之用。

一、金刚钻:(线性布局,英文名 linearLayout)

布局xml文件中统大者是一个线性布局,它的长宽都已经fill_parent沾满了整个屏幕,因此想再在线性布局外再搞一个控件是不可能的。如果将统大者线性布局的高度设置为wrap_content(我称之为适可而止)也不行,这点我已经做过实验。不过,可以在统大者线性布局中搞其他布局与控件是可以的;

线性布局设置的是垂直方向,控件之间的关系是上下排列,一般人拿着手机是竖方向,也就是垂直方向;若切换为横屏,此时是水平方向,控件之间依然是上下排列。不管线性布局设置为垂直,还是水平,控件之间都是上下排列。

二、几个布局xml文件

1. look

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 2"/>
</LinearLayout>

对应的效果:

可以看到,第二个文本框并没有显示。我分析,线性布局沾满整个屏幕,两个文本框是水平排列在同一行,第一个文本框的宽度是沾满一整行,第二个文本框被挤压出界面,离开我们的视线。

2. look

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<TextView
android:layout_width="10px"
android:layout_height="wrap_content"
android:text="I am textview 1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 2"/>
</LinearLayout>

效果图

第一个文本框的宽度改为10px,因此,两个文本框此时是可以同行显示。第一个文本框的宽度是10px,因此无法在同一行显示所有的文本,还好高度是适可而止的,其余的文本就一个个向下显示。第二个文本框的宽度是横着沾满整个屏幕的(不过没有覆盖第一个文本的I字符,我的想象中第二个文本的I字符会与第一个文本框的I字符重叠),它的文本是可以全部显示在一行上。

3. look

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<TextView
android:layout_width="30px"
android:layout_height="wrap_content"
android:text="I am textview 1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 2"/>
</LinearLayout>

效果图

第一个文本的宽度改为30px,它的文本可以在一行中多显示几个字符,应该是4个字符(我没理解为什么第一行仍然只显示一个字符I,我认为am也应该显示在第一行上)。第二个文本框就是横着沾满一个屏幕,但是仍然没有覆盖掉第一个文本,否则它的文本第一个字符I应该与第一个文本框的首个字符I重叠,它所有的文本是从第31个px开始显示吧。

统大者线性布局,一般是要沾满整个屏幕,如果是沾满整个屏幕的话,那么它的方向的垂直与水平决定了布局中的子元素是垂直排列还是横向排列,不是说这个布局是垂直得看还是水平地看。

4. look

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 2"/>
</LinearLayout>

效果图

两个按钮的宽度沾满整个行(默认左右上下预留点空隙),高度适可而止。可以看到,下面整个屏幕就空间一片黑暗,都浪费了。

5. look

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am textview 1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am textview 2"/>
</LinearLayout>

效果图

两个按钮的高宽度均为适可而止,因此随着文本的多少恰当的显示;

6. look

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am textview 1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am textview 2"
android:layout_weight="1"/>
</LinearLayout>

效果图

第二个按钮给了它一个权重,这就意味着屏幕剩余的黑暗空间都给它了;第一个按钮的权重默认是0,所以按部就班的原样显示;

LinearLayout 线性布局支持为单独的子元素指定weight,允许子元素可以填充屏幕上的剩余空间,避免了在一个大屏幕中一串小对象挤成一堆的情况,允许他们放大填充空白。子元素指定一个weight值,剩余的空间就会按这些子元素指定的weight比例分配给这些子元素。默认的weight值为0。线性布局的方向默认是横向的。

三、暂时总结

对linearlayout线性布局的研究暂时就这么多,我想可能还远远不够。吃透线性布局还需要进一步的研究,在实战中研究,因此这篇博文还需要完善和扩充。

android_layout_linearlayout(一)的更多相关文章

  1. android_layout_linearlayout(二)

    android的线性布局linearlayout的研究没有尽头.看了官网关于线性布局的一个例子,捣鼓一阵,发现的迷惑记录在此. 一.先看看官网xml <?xml version="1. ...

随机推荐

  1. UWP项目生成错误: 未能使用“CompileXaml”任务的输入参数初始化该任务。“CompileXaml”任务不支持“PlatformXmlDir”参数。请确认该参数存在于此任务中,并且是可设置的公共实例属性。

    UWP项目生成错误: 未能使用“CompileXaml”任务的输入参数初始化该任务.“CompileXaml”任务不支持“PlatformXmlDir”参数.请确认该参数存在于此任务中,并且是可设置的 ...

  2. 创建dll动态链接库,并使用java调用

    参考文章:http://www.cnblogs.com/matthew-2013/p/3480296.html http://blog.csdn.net/g710710/article/details ...

  3. Android零基础入门第88节:Fragment显示和隐藏、绑定和解绑

    在上一期我们学习了FragmentManager和FragmentTransaction的作用,并用案例学习了Fragment的添加.移除和替换,本期一起来学习Fragment显示和隐藏.绑定和解绑. ...

  4. 一小部分机器学习算法小结: 优化算法、逻辑回归、支持向量机、决策树、集成算法、Word2Vec等

    优化算法 先导知识:泰勒公式 \[ f(x)=\sum_{n=0}^{\infty}\frac{f^{(n)}(x_0)}{n!}(x-x_0)^n \] 一阶泰勒展开: \[ f(x)\approx ...

  5. Qt 5.8 for Device Creation(好多内容,包括虚拟机安装,静态编译)

    http://doc.qt.io/QtEnterpriseEmbedded/qt-configuration-tool.html http://doc.qt.io/QtEnterpriseEmbedd ...

  6. 枚举当前系统用户(使用NetUserEnum API枚举)

    using System.Runtime.InteropServices;   [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unico ...

  7. Boyer-Moore字符串查找算法的实现

    前段时间在园子里看到一篇讲Boyer-Moore算法原理的文章http://kb.cnblogs.com/page/176945/,写的很详细,于是在这里自己写个C语言的实现,权当是练手吧. 基本思路 ...

  8. git push 时 failed to push some refs 的解决方案

    我们在利用 GIt 上传代码的时候,往往会遇到这样一个问题,导致我们的代码没有办法正常上传到仓库中 造成这个问题的原因其实很简单,就是因为远程仓库和本地库不一致. 基于这样的一个问题,解决办法自然也就 ...

  9. 教妹子用IDEA创建web应用,部署到Tomcat服务器

    自从上一篇原创发表之后,粉丝反应热烈.主要分两派,一派关注技术的,觉得看了那么多的公众号文章,终于找到一篇能看懂的了,于是沾沾自喜.另一派是关注妹子的,感叹自己空有一身绝技,公司里却无妹子可教,大喊可 ...

  10. 一道关于String的面试题,新鲜出炉,刚被坑过,趁热!!

    很多人都会答错的一道关于String的题目,究竟有什么难度? 我们一起来看一道关于String的面试题,准确说是改编的面试题! 准备好啦?在放大招之前先来一个小招式 String s1 = new S ...