【Android开发学习笔记】【第七课】五大布局-上

时间:2023-03-09 08:32:07
【Android开发学习笔记】【第七课】五大布局-上

概念


Android程序各式各样,依靠的就是布局,先来看看布局都是怎么来的:

【Android开发学习笔记】【第七课】五大布局-上

白色部分就是我们经常用的几种布局,主要说说介绍下面五大布局

FrameLayout

AbsoluteLayout

LinearLayout

RelativeLayout

TableLayout

先介绍两种:

线性布局-LinearLayout


在一个方向上对齐所有元素。

可以横着、竖着,也可以嵌套,直接看代码吧

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <!-- vertical 代表垂直布局 --> <TextView
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#88AAFF"
android:gravity="center_horizontal"
android:text="ABCDEFG"
android:textSize="14dip" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <!-- horizontal 代表横向布局 --> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="0"
android:background="#22FFFF"
android:text="hello" /> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="1"
android:background="#FF22FF"
android:text="world" /> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="2"
android:background="#2222FF"
android:text="ni" /> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="3"
android:background="#FFFF22"
android:text="hao" />
</LinearLayout> </LinearLayout>

根据上面的布局和程序运行的结果,可以得到如下结论:

1、android:orientation="vertical" 代表垂直布局   horizontal 则代表横向布局

2、android:gravity="center_horizontal" 这一句话使得 ABCDEFG 居中对齐

3、android:layout_weight="0"  这个值决定了占屏幕的百分比的权重

程序运行结果:

【Android开发学习笔记】【第七课】五大布局-上

看起来对于一般的需求,线性布局就够了。

相对布局-Relative Layout


听说是布局里面功能最强大的,它的存在是为了适应五花八门的屏幕分辨率。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <AnalogClock
android:id="@+id/aclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- layout_centerInParent="true" 居中显示,将这个控件显示在父窗口的中间位置. --> <DigitalClock
android:id="@+id/dclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/aclock"
android:layout_below="@id/aclock"
android:layout_marginLeft="40px" /> <!--
layout_alignLeft="@id/aclock" 将控件的左边缘和给定ID控件的左边缘对齐
android:layout_below="@id/aclock" 将控件置于给定ID控件之下
layout_marginLeft 定义的控件左边距为40个dip
--> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/aclock"
android:layout_toLeftOf="@id/dclock"
android:text="当前时间:" /> <!--
android:layout_above="@id/xxx" 将控件置于给定ID控件之上
android:layout_toLeftOf 将控件的右边缘和给定ID控件的左边缘对齐
--> </RelativeLayout>

根据上面注释里面给出来的就是,就可以知道程序运行起来之后是这样样子的:

【Android开发学习笔记】【第七课】五大布局-上

相对布局果然强大,可以随意布置,下面看一些常用的相对布局使用的属性的含义:

android:layout_above="@id/xxx"            --将控件置于给定ID控件之上
android:layout_below="@id/xxx"            --将控件置于给定ID控件之下
android:layout_toLeftOf="@id/xxx"         --将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx"        --将控件的左边缘和给定ID控件的右边缘对齐
android:layout_alignLeft="@id/xxx"        --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"         --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"       --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"      --将控件的底边缘和给定ID控件的底边缘对齐
android:layout_alignParentLeft="true"     --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"      --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"    --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true"   --将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true"      --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"    --将控件置于水平方向的中心位置
android:layout_centerVertical="true"      --将控件置于垂直方向的中心位置