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

一、金刚钻:(线性布局,英文名 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. delphi中使用词霸2005的动态库XdictGrb.dll实现屏幕取词

    近日来,在网上发现关于屏幕取词技术的捷径,搜索很长时间,发现实现方式以VB出现的居多,但是通过Delphi来实现的却好象没有看到,自己参考着VB的相关代码琢磨了一下通过delphi来实现的方式. 其实 ...

  2. VS2008发布项目“发布失败”,没有提示错误

    VS2008发布项目时发布失败,但是没有提示任何的错误. 解决方法: 组合键”Ctrl+Alt+O”; 根据这个我知道了,是因为我更改了文件名,发布时找不到导致的,然后在资源管理器中找到那一项,删除或 ...

  3. C# string.Format()格式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. HtmlAgilityPack开发

    官方网站: http://html-agility-pack.net/ Nuget安装: Install-Package HtmlAgilityPack C# HTML Parser Examples ...

  5. How to setup Assigned Access in Windows 10 (Kiosk Mode) 设置分配的访问权限(Kiosk模式)

    Let’s say you’re building some sort of ingenious mechanical contraption to be displayed in public th ...

  6. C#WebBroswer控件的使用

    在WebBroswer中可以嵌入一个网页文件,通过Url属性绑定. URI,统一资源标识符,用来唯一的标识一个资源. URL,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源. 它包 ...

  7. 毕设(三)NotifyIcon

    NotifyIcon是一个比较特殊的组件,其特殊之处是既可以把它归类到控件中,也可以把它归类到组件中.这是因为将其拖放到设计窗体后,我们并不能马上看到它的界面(像组件),而是在运行时才能看到它(像控件 ...

  8. UWP开发-自适应布局

    了解css的人知道,对于不同的屏幕尺寸,css使用一种名为媒体查询的东东来适用不同的屏幕尺寸,以提升用户体验.当用户使用PC等大屏幕的设备时,网页将呈现一种布局形式:而当用户使用手机等小屏幕设备时,布 ...

  9. 用 eric6 与 PyQt5 实现python的极速GUI编程(35篇PyQT和200多篇Python)

    [题记] 我是一个菜鸟,这个系列是我的学习笔记. PyQt5 出来有一段时间了, PyQt5 较之 PyQt4 有一些变化,而网上流传的几乎都是 PyQt4 的教程,照搬的话大多会出错. eric6 ...

  10. 使用BCP批量导入数据

    本文原创,转载请标明出处 BCP 工具的使用 The bulk copy program utility (bcp) bulk copies data between an instance of M ...