展会信息港展会大全

Android系统五大布局详解Layout
来源:互联网   发布日期:2015-10-13 15:21:26   浏览:1481次  

导读:我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的。组件就是我们常见的Button、Text...

我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的。组件就是我们常见的Button、TextEdit等等。那么我们平时看到的Android手机中那些漂亮的界面是怎么显示出来的呢?这就要用到Android的布局管理器了,网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。

在分析布局之前,我们首先看看控件:Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:第一种也是我们最常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根据配置文件来创建相应的View视图。第二种是我们在代码中直接使用相应的类来创建视图。

如何使用XML文件定义视图:

每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。布局文件一般以对应activity的名字命名,以 .xml 为后缀。在xml中为创建组件时,需要为组件指定id,如:android:id="@+id/名字"系统会自动在gen目录下创建相应的R资源类变量。

如何在代码中使用视图:

在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等。

如:

private Button btnSndMag;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);// 加载main.xml布局文件

btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通过id找到对于的Button组件

....

}

private Button btnSndMag;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main); // 加载main.xml布局文件

btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通过id找到对于的Button组件

....

}

下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。

(1)LinearLayout 线性布局

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation="vertical" 和 android:orientation="horizontal"来设置。

android:layout_weight 表示子元素占据的空间大小的比例,有人说这个值大小和占据空间成正比,有人说反比。我在实际应用中设置和网上资料显示的刚好相反,这个问题后面会专门写一篇文章来分析。现在我们只需要按照正比例来设置就可以。

例如下面我们实现一个如图所示的简易计算器界面:

代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF"

tools:context=".MainActivity" >

// 这里第一行显示标签为一个水平布局

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<EditText

android:id="@+id/msg"

android:inputType="number"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="">

</EditText>

</LinearLayout>

// 第二行为 mc m+ m- mr 四个Button构成一个水平布局

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="mc" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="m+" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="m-" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="mr" android:layout_weight="1">

</Button>

</LinearLayout>

// 同上 C +/-/ * 四个Button构成一个水平布局

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="C" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="+/-" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="/" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="*" >

</Button>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="7" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="8" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="9" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="-" android:layout_weight="1">

</Button>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="4" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="5" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="6" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="+" >

</Button>

</LinearLayout>

// 最外层是一个水平布局,由左边上面一行1 2 3三个Button,下面一行的0 . 两个Button 和 右边的=构成

<LinearLayout android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

// 这里 1 2 3 和 下面的 0 . 构成一个垂直布局

<LinearLayout android:orientation="vertical"

android:layout_weight="3"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

// 这里的 1 2 3 构成一个水平布局

<LinearLayout android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="1"></Button>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="2"></Button>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="3"></Button>

</LinearLayout>

// 这里的 0 和 . 构成一个水平布局,注意这里的android_weight参数设置

<LinearLayout android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:layout_width="0px"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="0"></Button>

<Button

android:layout_width="0px"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="."></Button>

</LinearLayout>

</LinearLayout>

// 这里一个单独Button构成的垂直布局

<LinearLayout android:orientation="vertical"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="match_parent">

<Button

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="="></Button>

</LinearLayout>

</LinearLayout>

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF"

tools:context=".MainActivity" >

// 这里第一行显示标签为一个水平布局

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<EditText

android:id="@+id/msg"

android:inputType="number"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="">

</EditText>

</LinearLayout>

// 第二行为 mc m+ m- mr 四个Button构成一个水平布局

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="mc" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="m+" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="m-" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="mr" android:layout_weight="1">

</Button>

</LinearLayout>

// 同上 C +/-/ * 四个Button构成一个水平布局

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="C" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="+/-" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="/" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="*" >

</Button>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="7" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="8" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="9" android:layout_weight="1">

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="-" android:layout_weight="1">

</Button>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="4" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="5" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="6" >

</Button>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="+" >

</Button>

</LinearLayout>

// 最外层是一个水平布局,由左边上面一行1 2 3三个Button,下面一行的0 . 两个Button 和 右边的=构成

<LinearLayout android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

// 这里 1 2 3 和 下面的 0 . 构成一个垂直布局

<LinearLayout android:orientation="vertical"

android:layout_weight="3"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

// 这里的 1 2 3 构成一个水平布局

<LinearLayout android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="1"></Button>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="2"></Button>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="3"></Button>

</LinearLayout>

// 这里的 0 和 . 构成一个水平布局,注意这里的android_weight参数设置

<LinearLayout android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:layout_width="0px"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="0"></Button>

<Button

android:layout_width="0px"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="."></Button>

</LinearLayout>

</LinearLayout>

// 这里一个单独Button构成的垂直布局

<LinearLayout android:orientation="vertical"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="match_parent">

<Button

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="="></Button>

</LinearLayout>

</LinearLayout>

</LinearLayout>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港