Android布局管理器的介绍与使用

来源:互联网 发布:xmind8 for mac序列号 编辑:程序博客网 时间:2024/05/18 02:51
package com.example.layout;/* * 布局管理器:控制各个组件在窗体中的位置和大小 * 管理器种类有如下5种: * 1、线性布局管理器(LinearLayout) * 2、表格布局管理器(TableLayout) * 3、帧布局管理器(FrameLayout) * 4、相对布局管理器(RelativeLayout) * 5、绝对布局管理器(AbsoluteLayout)[不推荐使用,其他可替代] */import android.os.Bundle;import android.app.Activity;/* * 线性布局管理器:将放入其中的组件横向或纵向排列其每行或列只有一个组件 * 注意1:线性布局不会换行,当组件一个挨着一个排列到窗体边缘后,剩下的组件将不会 * 显示出来 * 注意2:推荐使用XML布局文件中定义线性布局管理器 *  * 主要属性列举如下: * android:orientation指定管理器内组件的排列方式,可选值为horizontal(水平排列)和vertical(默认值,垂直排列) * android:gravity指定布局管理器内组件的对齐方式,其可选值为top/bottom/left/right/center_vertical等等,注意其值可以同时指定 * android:layout_width指定布局管理器该组件本身的宽度;其可选值为fill_parent/match_parent(两者等价)和wrap_content * android:layout_height指定布局管理器该组件本身的高度;其可选值为fill_parent/match_parent(两者等价)和wrap_content * android:id指定布局管理器该组件本身的id,这样Java代码中可以应用该属性引用这个组件,Java代码中通过findViewById()方法获取它 * android:background指定组件背景(图片或颜色) */public class LinearActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_linear);}}
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">        <Button android:text="Button1"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>     <Button android:text="Button2"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>      <Button android:text="Button3"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>    </LinearLayout>


<pre name="code" class="java">package com.example.layout;import android.app.Activity;import android.os.Bundle;/* * 表格布局管理器:以行列的形式来管理放入其中的UI组件 * <TableRow>标记占用一行,注意:在该标记中每天一个组件,表格就会增加一列 * 注意1:推荐使用在XML布局文件中定义表格布局管理器 * 注意2:在表格布局中,列可以被隐藏、伸展和强制收缩,从而与屏幕相适应 * 特别属性列举如下: * android:collapseColumns指定需要被隐藏的列的列序号(序号从0开始),多个列号用逗号隔开 * android:shrinkCloumns指定允许被收缩的列的列序号(从0开始),多个列号用逗号隔开 * android:stretchColumns指定允许被拉伸的列的序列号(从0开始),多个列号用逗号隔开 */public class TabActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tab);}}


<pre name="code" class="html"><TableLayoutandroid:layout_width="fill_parent" android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:gravity="center_vertical"android:stretchColumns="0,3"><!-- 第一行 --><TableRow  android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView/><TextView android:text="用户名:" android:id="@+id/textView1" android:layout_width="wrap_content"android:textSize="24px" android:layout_height="wrap_content"/><EditText android:textSize="24px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="200px"/><TextView />  </TableRow>  <!-- 第二行 --><TableRow  android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView/><TextView android:text="密    码:"android:textSize="24px" android:layout_width="wrap_content" android:layout_height="wrap_content"/><EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="24px" android:inputType="textPassword"/><TextView /></TableRow><!-- 第3行 --><TableRow android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView/><Button android:text="登录" android:layout_width="wrap_content" android:layout_height="wrap_content"/><Button android:text="退出"  android:layout_width="wrap_content" android:layout_height="wrap_content"/><TextView /></TableRow></TableLayout>


<pre name="code" class="java">package com.example.layout;import android.app.Activity;import android.os.Bundle;/* * 帧布局管理器:每加入一个组件,都会创建一个空白的区域,通常称为一帧,这些帧 * 都会根据gravity属性执行自动对齐。默认情况下,帧布局是从屏幕的左上角(0,0)坐标开始布局 * 多个组件层叠排序,后面的组件会覆盖前面的组件 * 注意1:推荐使用在XML布局文件中定义帧布局管理器 * 注意2:帧布局经常用于游戏开发中,用于显示自定义的视图 * 特殊属性如下: * android:foreground设置帧布局管理器的前景图像 * android:foregroundGravity设置前景图像显示的位置 */public class FrameActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_frame);}}


<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><FrameLayout     android:id="@+id/frameLayout1"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      xmlns:android="http://schemas.android.com/apk/res/android"     android:background="#FFF"     android:foreground="@drawable/icon"     android:foregroundGravity="bottom|right"     >          <!-- 添加居中显示的蓝色背景的TextView,将显示在最下层  -->    <TextView android:text="蓝色背景的TextView"     android:id="@+id/textView1"      android:background="#FF0000FF"     android:layout_gravity="center"     android:layout_width="360px"      android:layout_height="200px"/>         <!-- 添加居中显示的天蓝色背景的TextView,将显示在中间层  -->         <TextView android:text="天蓝色背景的TextView"     android:id="@+id/textView2"     android:layout_width="300px"     android:layout_height="150px"    android:background="#FF0077FF"    android:layout_gravity="center"    />         <!-- 添加居中显示的水蓝色背景的TextView,将显示在最上层  -->    <TextView android:text="水蓝色背景的TextView"     android:id="@+id/textView3"     android:layout_width="240px"     android:layout_height="100px"    android:background="#FF00B4FF"    android:layout_gravity="center"    />     </FrameLayout>


<pre name="code" class="java">package com.example.layout;import android.app.Activity;import android.os.Bundle;/* * 相对布局管理器:根据指定组件之间的相对的位置来进行布局 * 注意:推荐使用在XML布局文件定义布局管理器 * 属性部分列举如下: * android:gravity指定布局管理器中组件的对齐方式 * android:layout_above * android:layout_below * android:toLeftOf * android:toRightOf等等等 */public class RelativeActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_relative);}}


<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 添加顶部图片 --><ImageView android:layout_width="match_parent"android:layout_height="wrap_content" android:scaleType="centerCrop"android:layout_weight="1"android:src="@drawable/top" /><!-- 添加一个相对布局管理器 --><RelativeLayout android:layout_weight="2"android:layout_height="wrap_content" android:background="@drawable/bottom"android:id="@+id/relativeLayout1" android:layout_width="match_parent"><!-- 添中间位置的图片 --><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/imageButton0"android:src="@drawable/enter" android:layout_centerInParent="true" /><!-- 添加上方显示的图片 --><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/imageButton1"android:src="@drawable/setting" android:layout_above="@+id/imageButton0"android:layout_alignLeft="@+id/imageButton0" /><!-- 添加下方显示的图片 --><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/imageButton2"android:src="@drawable/exit" android:layout_below="@+id/imageButton0"android:layout_alignLeft="@+id/imageButton0" /><!-- 添加左侧方显示的图片 --><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/imageButton3"android:src="@drawable/help" android:layout_toLeftOf="@+id/imageButton0"android:layout_alignTop="@+id/imageButton0" /><!-- 添加右侧显示的图片 --><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/imageButton4"android:src="@drawable/board" android:layout_toRightOf="@+id/imageButton0"android:layout_alignTop="@+id/imageButton0" /></RelativeLayout></LinearLayout>



0 0
原创粉丝点击