android_layout_linearlayout(一)
这几天一直在研究线性布局这块,记录下一些研究心得,作为参考之用。
一、金刚钻:(线性布局,英文名 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线性布局的研究暂时就这么多,我想可能还远远不够。吃透线性布局还需要进一步的研究,在实战中研究,因此这篇博文还需要完善和扩充。
android_layout_linearlayout(一)的更多相关文章
- android_layout_linearlayout(二)
android的线性布局linearlayout的研究没有尽头.看了官网关于线性布局的一个例子,捣鼓一阵,发现的迷惑记录在此. 一.先看看官网xml <?xml version="1. ...
随机推荐
- Redis进阶实践之十八 使用管道模式提高Redis查询的速度
原文:Redis进阶实践之十八 使用管道模式提高Redis查询的速度 一.引言 学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中 ...
- windows下捕获dump之Google breakpad_client
breakpad是Google开源的一套跨平台工具,用于dump的处理.很全的一套东西,我这里只简单涉及breakpad客户端,不涉及纯文本符号生成,不涉及dump解析. 一.使用 最简单的是使用进程 ...
- asp.net网站在手机浏览器上全屏显示
前段时间要把asp.net 网站,在手机上全屏浏览,发现总是小小的一块,不能全屏 后来发现 JQuery Mobile 中在开头都用 <meta name="viewport&quo ...
- Linux下的软件安装
在线安装 APT:advanced packaging Tool,Debian及其派生的发行版的软件包管理工具,包含以apt-开头的多个工具,如apt-get,apt-cache,apt-cdrom ...
- SharePoint js操作原生的New/Edit表单
列表的表单,有个类似的需求:在New需隐藏特定字段,Edit时显示. 默认是New/Edit表单的字段是一样,就算在Content type 是隐藏也是同时影响两个表单. 如何做到仅仅在New时隐 ...
- xe5 for android 地理定位GPS
先上源码,在解释. implementation uses androidapi.jni.JavaTypes, androidapi.jni.Location, FMX.helpers.android ...
- 【操作系统】elementary OS 和 deepin OS
文章摘自:http://blog.sina.com.cn/s/blog_64fb59d90102x3xa.html 部分参考:https://bbs.deepin.org/forum.php?mod= ...
- Qt中由表中单元格的QModelIndex获取Global Pos的正确方法
一直在尝试从单元格的行列索引(QModelIndex)获取其单元格的全局坐标(Global Pos)的方法,以期待在指定单元格附近弹出帮助信息.由View中的columnViewportPositio ...
- spring boot单元测试之MockMvc
spring单元测试之MockMvc,这个只是模拟,并不是真正的servlet,所以session.servletContext是没法用的. @RunWith(SpringRunner.class) ...
- Python连载12-shutil模块
一.shutil模块 1.函数:copy() (1)用法:复制文件0 (2)格式:copy(来源路径,目标路径) (3)返回值:返回目标路径 (4)注意:拷贝的同时可以给文件重命名 source_pa ...