概念:
LinearLayout是一种线性布局,他会将控件在水平和垂直方向做线性排列

官方文档:


继承关系:


常用属性:
android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小  同一线性上当前控件所占比例长度



样例一:


代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_weight="4" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="send"
android:id="@+id/btn_send"
android:layout_weight="1" />
</LinearLayout>
android:layout_width="0dp"  使得宽度不依赖于layout_width
当前同一水平线上有2个控件  所以layout_weight = 4 表示其宽度占屏幕宽度的4/5


案例二:


LinearLayout嵌套


Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_weight="1" >

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="green"
android:textColor="#ff0000"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="black"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:background="#aaaa00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="unknown"
android:background="#00aaaa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<TextView
android:text="color_red"
android:gravity="fill_vertical"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<TextView
android:text="white"
android:textColor="#ff0000"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>

</LinearLayout>

案例三:

android:gravity:用来控制组件的对齐方式
每一个按钮独自占一列

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3"
android:layout_gravity="bottom|right|top"
android:layout_weight="1"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center_vertical"
android:layout_weight="1"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="bottom"
android:layout_weight="1" />
</LinearLayout>








LinearLayout的更多相关文章

  1. Android中的LinearLayout布局

    LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,  线性布局是按照垂直方向(vertical)或水平方向 ...

  2. 和我一起看API(一)你所不知道的LinearLayout补充

    楼主英语水平差,翻译的不好的话请多多指正,嘿嘿... A Layout that arranges its children in a single column or a single row. T ...

  3. Android-RelativeLayout(相对布局)、LinearLayout(线性布局)

    RelativeLayout(相对布局):按照各子元素之间的位置关系完成布局. 定位:android:layout_above="@id/xxx" --将控件置于给定ID控件之上 ...

  4. LinearLayout布局问题

    LinearLayout是平时开发中很常见的线性布局方式,分为水平和竖直2种,笔者在实际使用中发现了如下问题,希望能帮到别人. 横着的LinearLayout,凡是设置x坐标的属性都不起作用,比如la ...

  5. 动态添加LinearLayout的高度

    WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); int width ...

  6. 动态生成linearLayout

    LinearLayout linearLayout=new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL) ...

  7. Android的学习第六章(布局一LinearLayout)

    今天我们来说一下Android五大布局-LinearLayout布局(线性布局) 含义:线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 主要作用:主要对整个界面 ...

  8. This TableLayout layout or its LinearLayout parent is possibly useless

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  9. Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView

    Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView   The post "Im ...

  10. Andriod中textview垂直水平居中及LinearLayout内组件的垂直布局

    1.textview 垂直水平居中的设置 Android:gravity="center_vertical|center" 2.LinearLayout中设置控件垂直布局,默认的是 ...

随机推荐

  1. GLSL Debugger的姿势

    https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/OpenGLShaderBuilder ...

  2. swiper 增加同页面增加2个滚动

    js代码 <script type="text/javascript"> var mySwiper = new Swiper('.slide_a',{ noSwipin ...

  3. td:first-child 伪类 匹配第一个 匹配第一个 <td> 元素

    css代码: td:first-child{border-right: 1px solid #d9dbdd; } 备注:在下面的例子中,选择器匹配作为任何元素的第一个子元素的 p 元素: 链接:htt ...

  4. LoadRunner 11 安装破解

    安装前的准备: 下载地址: A:安装介质 loadrunner-11.iso:http://yunpan.cn/cHN7JpYPi57wL (提取码:79f0) lm70.dll:http://yun ...

  5. 在CentOS上搭建apache和PHP服务器环境(转)

    1.您也可以使用一键自动部署环境的工具,请参见网友开发的这个工具 http://www.centos.bz/2013/08/ezhttp-tutorial/ 2. 安装: wget -c http:/ ...

  6. 在VS2013中强制IIS Express应用程序池使用经典模式

    直接在文件夹地址栏输入 %userprofile%\documents\iisexpress\config\applicationhost.config 会打开上边的配置文件 将 <siteDe ...

  7. Python开发【第十七篇】:MySQL(一)

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...

  8. PHP与Javascript的混合测试

    js调用php <?php $num=88; ?> <script> var a = <?php echo $num;?>; alert(a); </scri ...

  9. C/C++/Java/C#语言的基本类型

    C语言的基本类型有:char, short ,int ,long ,float ,double 一共6种基本类型. C++语言有:bool, char ,wchar_t, short, int , l ...

  10. CxImage

    启动项目的时候显示此时 百度“无法启动程序cximage.lib” 得到http://tieba.baidu.com/p/1935208210把第二项设为启动项即可 为什么设置第二项为启动项呢 因为h ...