7.2 LinearLayout布局详解

简介: LinearLayout线性布局,线性布局是所有布局中最常用的,它可以让其中的子元素垂直或水平的方式排列(通过排列方向的设置)。通常复杂的布局都是在LinearLayout布局中嵌套而成的。<br>下面看一个LinearLayout的例子,这个例子中有垂直和水平的嵌套使用,例子如下图7-12所示。<br>  <br><img src="http://dl.iteye.com/upload/at
LinearLayout线性布局,线性布局是所有布局中最常用的,它可以让其中的子元素垂直或水平的方式排列(通过排列方向的设置)。通常复杂的布局都是在LinearLayout布局中嵌套而成的。
下面看一个LinearLayout的例子,这个例子中有垂直和水平的嵌套使用,例子如下图7-12所示。
 


图7-12 LinearLayout
布局文件请参考代码清单7-14,完整代码请参考chapter7_2工程中linearlayout2.xml代码部分(chapter7_2/res/layout/linearlayout2.xml)。
【代码清单7-14】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
android:textSize="20dip" android:gravity="center" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/user"
android:textSize="15dip" />
<EditText android:id="@+id/username" android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/pass"
android:textSize="15dip" />
<EditText android:id="@+id/password" android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="@string/loginbtn" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@string/registerbtn" android:id="@+id/Button02"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

</LinearLayout>

                                                                                        出自《Android开发案例驱动教程》第七章


目录
相关文章
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(五)线性布局 - LinearLayout
Android XML 布局基础(五)线性布局 - LinearLayout
85 0
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(六)相对布局 - RelativeLayout
Android XML 布局基础(六)相对布局 - RelativeLayout
104 0
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(七)帧布局 - FrameLayout
Android XML 布局基础(七)帧布局 - FrameLayout
54 0
|
容器
RelativeLayout(相对布局)
LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margi
62 0
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
50 0
|
XML Android开发 数据格式
Android开发之LinearLayout布局详解
Android开发之LinearLayout布局详解
267 0