Android之四大布局【LinearLayout,TableLayout,FrameLayout,RelativeLayout】

时间:2022-12-21 11:37:35

LinearLayout

线性布局是将放入其中的组件按照垂直(vertical)或者水平(horizontal)方向来布局,

也就是控制其中组件横向排列或者纵向排列。在线性布局中

每一行【针对垂直排列】或每一列【针对水平排列】只能放一个组件

注意:Android线性布局不会换行,当组件一个挨着一个排列到窗体边缘后

剩下的组件将不会显示出来


排列方式由android:orientation属性控制,对齐方式由android:gravity属性来控制

(1)常见属性:

android:orientation,    android:gravity,    

layout_width,     layout_height,

androud:id,android:background


其中前两个是线性布局管理器支持的属性,

后四个是android.view.View和android.view.ViewGroup支持的属性

android:orientation属性

用于设置布局管理器内组件的排列方式,其可选值有horizontal(水平), vertical 【默认】(垂直)


android:gravity属性

用于设置布局管理器内的组件对齐方式,其可选值:top, bottom,left,right,center_vertical,fill_vertical,center_horizontal,fill_horizontal,center,fill,clip_vertical,clip_horizontal

这些值可以同时设定中间用|隔开 。例如:要指定组件靠右下角对齐,可以使用reight|bottom


android:layout_weight是ViewGroup.LayoutParams支持的XML属性

属性用于设置该组件的基本宽度,其可选值有fill_parent,match_parent,wrap_content,其中match_parent(从Android2.2开始推荐使用)和_parent的作用完全相同【表示该组件的宽度和父亲、容器宽度相同】

wrep_content表示该组件宽度恰好能包裹他的内容。

android:layout_height

android:layout_weight类似



TableLayout

表格布局与常见表格类似,它以行,列的形式来管理放入其中的UI组件。表格布局使用<TableLayout></TableLayout>标记定义,

在表格中,可以添加多个<TableRow>标记,每个<TableRow>标记占用一行,由于<TableRow>标记也是容器,所以该标记中还可以添加

其他组件,在<TableRow>标记中,每添加一个组件,表格就会增加一列。

在表格布局中,通过设置可以对列进行隐藏或延伸操作。

从而填充可利用的屏幕空间,也可以设置强制收缩,直到表格匹配屏幕大小。


TableLayout

继承了LinearLayout,因此它支持LinearLayout所支持的全部XML属性,此外TableLayout还有一些特殊属性:

 android:stretchColumns设置需要隐藏的列的序列号(序列从0开始),多个列序号之间用逗号隔开

android:shrinkColumns设置允许被收缩的列序号(序列从0开始),多个列序号之间用逗号隔开

android:stretchColumns设置允许被拉伸的列序号(序列从0开始),多个列序号之间用逗号隔开


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tablelayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loginbg"
android:gravity="center_vertical"
android:stretchColumns="0,3" ><pre name="code" class="html"> <!--<pre name="code" class="html">android:stretchColumns="0,3"
第一列和第四列设置允许被拉伸,是为了让用户登录表单在水平方向上居住显示
-->
<!-- 第一行 --> <TableRow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="200px" android:textSize="24px" /> <TextView /> </TableRow> <!-- 第二行 --> <TableRow android:id="@+id/tablerow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView /> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textSize="24px" /> <EditText android:id="@+id/edittext2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:minWidth="200px" android:textSize="24px" /> <TextView /> </TableRow> <TableRow android:id="@+id/tablerow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView /> <TextView /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:textSize="24px" /> <TextView /> </TableRow></TableLayout> 
 

RelativeLayout(相对布局)

相对布局是按照组件之间的相对位置进行布局的,如:某个组件在另一个组件的左边,右边,上方,下方等

常用属性:

android:gravity 用于设置布局管理器中各子组件的对齐方式

android:ignoreGravity 用于指定哪个组件不受gravity的影响


另外:RelativelyLayout 提供一个RelativeLayout.LayoutParams的内部类,通过该类提供的大量XML属性更好的控制相对布局中各个组件之间的

分布方式:


见下面的例子:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >

<!-- 添加顶部图片 -->

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:scaleType="centerCrop" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100px"
android:layout_marginTop="10px"
android:text="用户名:"
android:textColor="#FF00FF"
android:textSize="30px" />

<EditText
android:id="@+id/edittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textview1"
android:minWidth="200px"
android:textSize="30px" />

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:layout_marginLeft="100px"
android:layout_marginTop="30px"
android:text="密 码:"
android:textColor="#FF00FF"
android:textSize="30px" />

<EditText
android:id="@+id/edittext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textview2"
android:inputType="textPassword"
android:minWidth="200px"
android:textSize="30px" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30px"
android:background="@drawable/loginone"
android:textSize="35px" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/button1"
android:layout_below="@id/textview2"
android:layout_marginTop="30px"
android:layout_toRightOf="@id/button1"
android:paddingBottom="0px"
android:text="注册"
android:textColor="#FF00FF"
android:textSize="25px" />
</RelativeLayout>

</LinearLayout>