---
title: "Android界面布局"
author: "hmeng"
date: "2014-08-04T16:43:57Z"
tags: []
categories: ["编程语言"]
slug: "android"
draft: false
lastmod: "2014-08-04T16:57:37Z"
id: 2100133
url: "https://hmeng.iteye.com/blog/2100133"
---

这几天手机安卓开发的学习，让我更加感受到了JAVA的魅力。
     了解掌握了安卓开发软件的使用，接下来关键的第一步就是设计界面了。
     界面设计包括布局和组件，组件按布局要求排列形成界面，而安卓的布局有以下五大布局：
【1】FrameLayout 框架布局，是布局文件中默认的最简单的布局。
   所有添加到这个布局中的视图都以层叠的方式显示，且组件均显示在屏幕的左上角。第一个添加的控件被放在最底层，最后一个添加到框架布局中的视图显示在最顶层，上一层的控件会覆盖下一层的控件。在非常简单的界面中常用。

【2】LinearLayout 线性布局
   在一个方向上(垂直或水平)对齐所有子元素，一个垂直列表每行将只有一个子元素(无论它们有多宽)，一个水平列表只是一列的高度（最高子元素的高度来填充）。是常用的布局。
垂直线性布局：android:orientation="vertical" 
水平线性布局：android:orientation="horizontal" 
两者可嵌套使用。
例如：一个登录界面

```xml

<!--总体布局设计为垂直线性布局-->
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_username"/>
<EditText 
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>

<!--嵌套水平布局，放置文本框和密码输入框-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_password" />
<EditText 
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</LinearLayout>

<!--登录按钮-->
<Button 
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_login" />
</LinearLayout>
```


【3】RelativeLayout 相对布局
相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。参照控件可以是父控件，也可以是其它子控件，但是被参照的控件必须要在参照它的控件之前定义。个人觉得这种布局和线性布局比较好用,可常用到。
例如：

```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ttkupao.MainActivity"
    tools:ignore="MergeRootFrame" >
<ImageButton
    android:id="@+id/jump"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/touch2" 
    android:onClick="click"/>
<ImageButton
    android:id="@+id/down"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@drawable/touch" 
    android:onClick="click"/>
</RelativeLayout>
```

这是两个按钮放在屏幕的左下角和右下角位置，均是以父控件为参考的，这时没有必要将子控件之间再增加相对关系。
下面是相对布局的一些重要属性：
第一类:属性值为true或false
android:layout_centerHrizontal                       水平居中
android:layout_centerVertical                        垂直居中
android:layout_centerInparent                      相对于父元素完全居中
android:layout_alignParentBottom                     贴紧父元素的下边缘
android:layout_alignParentLeft                       贴紧父元素的左边缘
android:layout_alignParentRight                      贴紧父元素的右边缘
android:layout_alignParentTop                        贴紧父元素的上边缘
android:layout_alignWithParentIfMissing             如果对应的兄弟元素找不到的话就以父元素做参照物

第二类：属性值必须为id的引用名“@id/id-name”
android:layout_below                          在某元素的下方
android:layout_above                          在某元素的的上方
android:layout_toLeftOf                       在某元素的左边
android:layout_toRightOf                     在某元素的右边
android:layout_alignTop               本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft              本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom            本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight             本元素的右边缘和某元素的的右边缘对齐

第三类：属性值为具体的像素值，如30dip，40px
android:layout_marginBottom              离某元素底边缘的距离
android:layout_marginLeft                   离某元素左边缘的距离
android:layout_marginRight                 离某元素右边缘的距离
android:layout_marginTop                   离某元素上边缘的距离

【4】TableLayout(表格布局)
表格布局模型以行列的形式管理子控件，每一行为一个TableRow的对象，当然也可以是一个View的对象。TableRow可以添加子控件，每添加一个为一列，子组件全部按照水平线性排列，并且宽（match parent）高(wrap parent)一致，这样所有组件就像是排列在一个表格中，单元格可以为空，但是不可以跨列

【5】AbsoluteLayout(绝对布局)
 android:layout_x="Xdp", android:layout_x="Xdp"指定组件的位置，即子控件需要指定相对于此坐标布局的横纵坐标值。由于手机应用需要适应不同的屏幕大小，而这种布局模型不能自适应屏幕尺寸大小，所以应用的相对较少。

具体的布局设计需要根据具体情况来设计，线性布局和相对布局是最常用到的。另外绘图的组件View或surfaceView若需要显示在其他组件之下，可在其他组件之前添加，否则可能被其他组件覆盖。