`
jackliao
  • 浏览: 77934 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Android的Layout完全介绍

阅读更多

1.FrameLayout

FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。


FrameLayout is the simplest type of layout object. It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Subsequent child views will simply be drawn over previous ones, partially or totally obscuring them (unless the newer object is transparent).
FrameLayout默认填充widget等在左上角,然后后面的控件遮住前面的,比如说有两个TextView,Text内容分别是I am textview 1 和
I am textview 2 看到的效果会如下:
?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="htHTMLtp://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="I am textview 1"
    />
    <TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="I am textview 2"
    />
    
</FrameLayout>

2.LinearLayout

LinearLayout以你为它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。 
LinearLayout 还支持为单独的子元素指定weight。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤成一堆的情况,而是允许他们放大填充空白。子元素指定一个weight值,剩余的空间就会按这些子元素指定的weight比例分配给这些子元素。默认的weight值为0。例如,如果有三个文本框,其中两个指定了weight值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大。
下面的两个窗体采用LinearLayout,包含一组的元素:一个按钮,几个标签,几个文本框。两个窗体都为布局做了一番修饰。文本框的width被设置为FILL_PARENT;其它元素的width被设置为WRAP_CONTENT。默认的对齐方式为左对齐。左边的窗体没有设置weight(默认为 0);右边的窗体的comments文本框weight被设置为1。如果Name文本框也被设置为1,那么Name和Comments这两个文本框将会有同样的高度。 

在一个水平排列的LinearLayout中,各项按他们的文本基线进行排列(第一列第一行的元素,即最上或最左,被设定为参考基线)。因此,人们在一个窗体中检索元素时,就不需要七上八下地读元素的文本了。我们可以在layout的XML中设置 android:baselineAligned="false",来关闭这个设置。



LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity(right, center, or left alignment) of each child.
LinearLayout是Android sdk创建project时的默认Layout,支持两种方式,默认是垂直vertical的线性layout,另一个是水平horizontal方向的线性排列。
效果如下

<?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="I am textview 1"
    />
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="I am textview 2"
    />
   
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="10px"
    android:layout_height="fill_parent"
    android:text="I am textview 1"
    />
    <TextView  
    android:layout_width="10px"
    android:layout_height="wrap_content"
    android:text="I am textview 2"
    />
    
</LinearLayout>

3.RelativeLayout
RelativeLayout允许子元素指定他们相对于其它元素或父元素的位置(通过ID指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用XML来指定这个layout,在你定义它之前,被关联的元素必须定义。 
这是一个RelativeLayout例子,其中有可视的和不可视的元素。基础的屏幕layout对象是一个RelativeLayout对象。

这个视图显示了屏幕元素的类名称,下面是每个元素的属性列表。这些属性一部份是由元素直接提供,另一部份是由容器的LayoutParams成员(RelativeLayout的子类)提供。RelativeLayout参数有 width,height,below,alignTop,toLeft,padding和marginLeft。注意,这些参数中的一部份,其值是相对于其它子元素而言的,所以才RelativeLayout。这些参数包括toLeft,alignTop和below,用来指定相对于其它元素的左,上和下的位置。

 

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position other view objects) must be listed in the XML file before you refer to it from the other views via its reference ID.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding = "10px"
    >
<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"
    android:layout_marginLeft = "150px"
    />
   <Button
   android:id="@+id/Button02" android:layout_width="wrap_content" android:text="Ok"
   android:layout_height="wrap_content"
   android:layout_marginLeft = "240px"
   android:layout_marginTop = "40px"
   ></Button>
   <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:text="Cancel" android:layout_height="wrap_content"
   android:layout_marginLeft = "160px"
   android:layout_marginTop = "40px"
   ></Button>



</RelativeLayout
4.AbsoluteLayout

AbsoluteLayout可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用AbsoluteLayout,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。

 


5.TableLayout

TableLayout将子元素的位置分配到行或列中。android的一个TableLayout由许多的TableRow组成,每个TableRow都会定义一个row(事实上,你可以定义其它的子对象,这在下面会解释到)。TableLayout容器不会显示row、cloumns或cell的边框线。每个row拥有0个或多个的cell;每个cell拥有一个 View对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与HTML中的不一样。下图显示了一个TableLayout,图中的虚线代表不可视的单元格边框。 


列可以被隐藏,也可以被设置为伸展的从而填充可利用的屏幕空间,也可以被设置为强制列收缩直到表格匹配屏幕大小。对于更详细信息,可以查看这个类的参考文档。 

TableLayout positions its children into rows and columns. TableLayout containers do not display border lines for their rows, columns, or cells. The table will have as many columns as the row with the most cells. A table can leave cells empty, but cells cannot span columns, as they can in HTML.
TableRow objects are the child views of a TableLayout (each TableRow defines a single row in the table). Each row has zero or more cells, each of which is defined by any kind of other View. So, the cells of a row may be composed of a variety of View objects, like ImageView or TextView objects. A cell may also be a ViewGroup object (for example, you can nest another TableLayout as a cell).
TableLayout和HTML的<table>基本上类似,还提供TableRow class, 直接等价与<tr>
 比如说要做成一个类似下面的HTML叶面
I am textview1 I am textview2
[OK button]
[Cancel button]
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    
 >
 <TableRow android:background="@drawable/a1">
    <TextView
        android:text="I am textview 1" />
    <TextView  
        android:paddingLeft = "20px"        
        android:width = "200px"
        
        android:text="I am textview 2"
    />
    </TableRow>
    <TableRow android:background="@drawable/a1">
   <Button
   android:id="@+id/Button02"  android:text="Ok"
     ></Button>
   <Button android:id="@+id/Button01" android:text="Cancel"
   ></Button>

</TableRow>

</TableLayout>

分享到:
评论

相关推荐

    Android自定义Layout布局

    本篇文章将深入探讨如何在Android中进行自定义Layout布局的开发。 首先,创建自定义Layout首先要创建一个新的Java类,继承自`ViewGroup`或`LinearLayout`等现有布局。选择`ViewGroup`是因为它是所有布局的基础类,...

    Android_layout.rar_android_android 布局_layout

    10. **Visibility属性**:`android:visibility`用于控制视图的可见性,可设为`visible`(默认,可见)、`gone`(完全不可见,不占用空间)或`invisible`(不可见,但保留空间)。 通过深入理解和灵活运用这些布局...

    Android Layout 英文版

    Android界面布局(Layout)是应用界面开发中的核心组件,它负责按照设计要求组织和展示应用界面中的组件。在Android开发中,布局用来确定UI组件如按钮、文本框和图片等在屏幕上的位置和排列方式。Google在2011年1月...

    dynamicLayout_android源码_

    在Android开发中,动态加载布局(Dynamic Layout)是一种常见的需求,它允许应用程序在运行时根据用户行为或特定条件改变UI布局。Android系统提供了一些机制来实现这种灵活性,其中包括使用LayoutInflater、ViewStub...

    Android中UI布局Layout

    在Android应用开发中,UI设计是至关重要的,而布局(Layout)则是构建用户界面的核心元素。布局决定了应用程序中各个组件的排列方式和相互关系。以下是关于Android中几种主要布局的详细解析: 1. **线性布局...

    android layout自动缩放扩大图片

    在Android开发中,布局(Layout)是构建用户界面的关键元素,而ImageView组件常用来展示图片。当我们在Android应用中加载图片时,有时需要对图片进行自动缩放,以适应不同的屏幕尺寸和分辨率,确保用户体验良好。...

    Android属性大全

    android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴紧父元素的右...

    Android高级应用源码-Android Layout UI 首页加载过渡动画,星期变化动画.zip

    这个"Android高级应用源码-Android Layout UI 首页加载过渡动画,星期变化动画.zip"的压缩包包含了一些高级的UI设计技巧,主要关注首页加载过渡动画以及星期变化动画的实现。通过分析这些源码,我们可以深入理解...

    android XML文件详解

    本文将详细介绍各种Layout中常用的一些重要属性,并将其分为三类进行阐述:定位与对齐、间距与填充、文本与图像处理。 #### 一、定位与对齐 1. **水平与垂直居中** - `android:layout_centerHorizontal` 和 `...

    android 点击button控制面板layout的显示消息

    首先,我们来看“android layout的显示消失”。在Android中,布局(Layout)是构成用户界面的基本元素,它包含了多个视图(View)或视图组(ViewGroup)。为了控制布局的可见性,我们可以利用View类中的三个常量:...

    android布局属性RelativeLayout详解.

    3. android:layout_centerInParent:使当前控件相对于父元素完全居中。 4. android:layout_alignParentBottom:使当前控件贴紧父元素的下边缘。 5. android:layout_alignParentLeft:使当前控件贴紧父元素的左边缘。...

    AndroidXML布局属性详解

    * android:layout_centerInParent:使控件相对于父控件完全居中。 * android:layout_alignParentBottom:使控件贴紧父控件的下边缘。 * android:layout_alignParentLeft:使控件贴紧父控件的左边缘。 * android:...

    Android自定义toolbar布局

    本教程将详细介绍如何在Android项目中自定义`Toolbar`布局。 首先,我们从`Toolbar`的基本概念开始。`Toolbar`是`android.support.v7.widget.Toolbar`类的一个实例,它是Android Support Library的一部分。它的引入...

    Android实例开发完全手册_part1

    《Android实例开发完全手册》是为开发者提供的一本详尽且实用的指南,它涵盖了Android应用开发的基础到高级技巧。本部分(part1)主要关注Android开发的基础知识,旨在帮助初学者快速上手并理解Android应用程序的...

    Android实例开发完全手册

    ### Android实例开发完全手册知识点概览 #### 第1章 正式开发前的准备 - **1.1 Windows下安装Java环境JDK** - **实例说明:** 本节介绍如何在Windows操作系统上安装Java Development Kit (JDK),这是进行Android...

    android布局属性详解.

    - **`android:layout_centerInParent`**:当设置为`true`时,组件将根据其父容器的位置完全居中(同时水平和垂直居中)。 - **`android:layout_alignParentBottom`**:当设置为`true`时,组件将贴紧其父容器的底部...

Global site tag (gtag.js) - Google Analytics