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. ...
随机推荐
- PySide——Python图形化界面入门教程(五)
PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...
- 百度官方wormHole后门检测记录
乌云地址:http://drops.wooyun.org/papers/10061 后门端口:40310/6259 本次测试在Ubuntu下,具体adb调试工具参考 sink_cup的博客 http: ...
- web-dev-server配置
webpack-dev-server是一个小型的Node.js Express服务器,它使用webpack-dev-middleware来服务于webpack的包,除此自外,它还有一个通过Sock.j ...
- oracle 12c连接pdb
12c中,如何连接pluggable database: 使用默认的service连接pdb,创建pdb之后,在监听中自动添加以pdb为名的service: 用户在cluster中创建service, ...
- UWP的TextBox和PasswordBox使用输入范围更改触摸键盘InputScope
原文:UWP的TextBox和PasswordBox使用输入范围更改触摸键盘InputScope 当你的应用运行在具有触摸屏的设备上时,触摸键盘可用于文本输入.当用户点击可编辑的输入字段(如 Text ...
- uwp汉堡菜单的实现
---恢复内容开始--- 现在uwp上面的汉堡菜单(就是那个三道杠,点击之后会出现菜单)使用的越来越普遍,比如微软自己家的Cortana.现在我使用的实现方法是使用SplitView实现.首先Spli ...
- Qt技术优势
1. Qt这个C++的图形库由Trolltech在1994年左右开发.它可以运行在Windows,Mac OS X, Unix,还有像Sharp Zaurus这类嵌入式系统中.Qt是完全面向对象的. ...
- <iOS 导航栏>第一节:导航栏透明方法实现代码
说下导航栏的透明方法: 很多应用需要导航栏随着向上滑动,逐渐从透明变成不透明,很炫酷,大部分应用都在使用导航栏渐变效果,现附上代码然后直接将实现,一会讲下如何来实现,这一部分直接上代码. ...
- 【Web前端Talk】“用数据说话,从埋点开始”-带你理解前端的三种埋点
埋点到底是什么呢? 引用自百科的原话是,埋点分析网站分析的一种常用的数据采集方法.因此其本质是分析,但是靠什么分析呢?靠埋点得到的数据.通俗来讲,就是当我想要在某个产品上得到用户的一些行为数据用来分析 ...
- netty中的发动机--EventLoop及其实现类NioEventLoop的源码分析
EventLoop 在之前介绍Bootstrap的初始化以及启动过程时,我们多次接触了NioEventLoopGroup这个类,关于这个类的理解,还需要了解netty的线程模型.NioEventLoo ...