Android_布局

来源:互联网 发布:货车找活都用什么软件 编辑:程序博客网 时间:2024/06/05 06:16
Android提供了5种类型的布局类型:

 1:LinearLayout(线性布局)流式布局

 Tip:下一个控件的坐标原点由上一个控件来决定,你可以沿水平方向或者垂直方向上来排列你的控件。 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context="com.example.lizelu.userinterfacedemo.MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><!--垂直线性布局方式--><LinearLayoutandroid:layout_width="pt"android:layout_height="match_parent"android:background="#ff"android:orientation="vertical"></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="pt"android:background="#ff"android:orientation="horizontal"></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#ff"android:orientation="horizontal"></LinearLayout></LinearLayout></LinearLayout></RelativeLayout>
View Code

 

 2:RelativeLayout(相对布局)Activity默认布局方式

  Tip:相对布局可以根据已经固定的控件来确定其他新加控件的位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.lizelu.userinterfacedemo.MainActivity"><Buttonandroid:id="@+id/button_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="center"/><Buttonandroid:id="@+id/button_above"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/button_center"android:layout_centerInParent="true"android:text="above"/><Buttonandroid:id="@+id/button_below"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button_center"android:layout_centerInParent="true"android:text="below"/><Buttonandroid:id="@+id/button_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@+id/button_center"android:layout_centerVertical="true"android:text="left"/><Buttonandroid:id="@+id/button_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/button_center"android:layout_centerVertical="true"android:text="right"/></RelativeLayout>
View Code

 3:TableLayout(表格布局)

  Tip:表格布局中,整个页面就相当于一张大的表格,控件就放在每个Cell中

<TableLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:stretchColumns=""><TableRow><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入用户名"/></TableRow><TableRow><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码:"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入密码"android:inputType="textPassword"/></TableRow><TableRow><Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:text="登录"android:layout_span=""/></TableRow></TableLayout>
View Code

 4:AbsoluteLayout(绝对布局)

  Tip:指定组件的左上角绝对坐标来指定组件的布局

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">  <Button android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="按钮1"    android:layout_x="0px" android:layout_y="0px" />  <Button android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="按钮2"    android:layout_x="20px" android:layout_y="20px" />  <Button android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="按钮3"    android:layout_x="40px" android:layout_y="40px" />  <Button android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="按钮4"    android:layout_x="60px" android:layout_y="60px" /></AbsoluteLayout>
View Code

 5:FrameLayout(单帧布局)

  Tip:FrameLayout中的Frame和iOS中的Frame不是一个概念,在iOS中的Frame你可以指定任意的坐标,而这个坐标点时相对于父视图的。FrameLayout中的Frame的坐标原点是屏幕的左上角,位置固定,你只需为控件指定大小即可

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context="com.example.lizelu.userinterfacedemo.MainActivity"><FrameLayoutandroid:layout_width="pt"android:layout_height="pt"android:background="#ff"><FrameLayoutandroid:layout_width="pt"android:layout_height="pt"android:background="#ff"></FrameLayout><FrameLayoutandroid:layout_width="pt"android:layout_height="pt"android:background="#ff"></FrameLayout><FrameLayoutandroid:layout_width="pt"android:layout_height="pt"android:background="#ffff"></FrameLayout><FrameLayoutandroid:layout_width="pt"android:layout_height="pt"android:background="#"></FrameLayout></FrameLayout></RelativeLayout>
View Code