目前我们新接触到了新的学习知识点—Android的界面编程。在接触这个Android编程的时候,对一些布局是要有一定的了解的,而且也挺重要的,在这段的学习中呢,我们就接触到最多的就是LinearLayout这个布局啦,叫做线性布局,是所有布局中最常用的类之一 ,我通过查阅资料得知Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局),因为对其他的布局话不是很了解,只能简单的描述一下:
线性布局 LinearLayout:线性布局是所有布局中最常用的类之一,LinearLayout可以让它的子元素垂直(android:rientation=”vertical”)或水平(android:rientation=”vertical”)的方式排成一行(不设置方向的时候默认按照垂直方向排列)。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列,还有就是在这个LinearLayout的weight(权重属性),这个在学习过程中用的挺多的。
相对布局 RelativeLayout 允许子元素指定他们相对于其它元素或父元素的位置(通过ID 指定)。
表格布局 TableLayout:将子元素的位置分配到行或列中。一个TableLayout由许多的TableRow 组成。表格由列和行组成许多的单元格。
帧布局 FrameLayout:是简单的一个布局对象。它可以被定制为你屏幕上的一个空白备用区域
网格布局 GridLayout:Android 4.0以后引入的一个新的布局,和前面说的TableLayout(表格布局) 有点类似,不过他有TableLayout很多没有的东西,也更好用。
绝对布局 AbsoluteLayout:容器中控件的位置大小,时通过指定X、Y坐标指定组件的位置,可是因为APP的手机屏幕大小,分辨率也会不同,一般这个布局都不会用到的啦。
下面我就用了FrameLayout做了个简单的栗子:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:foreground="@drawable/image"
android:foregroundGravity="left|bottom">
<TextView
android:layout_width="260dp"
android:layout_height="200dp"
android:background="#FF6143" />
<TextView
android:layout_width="180dp"
android:layout_height="200dp"
android:background="#7BFE00" />
<TextView
android:layout_width="90dp"
android:layout_height="200dp"
android:background="#FFFF00" />
</FrameLayout>
效果如下:
这个frameLayout就是三个TextView设置不同大小与背景色,依次覆盖,接着右下角的是前景图像,通过 android:foreground="@drawable/image"设置前景图像的图片, android:foregroundGravity="right|bottom"设置前景图像的位置在右下角。