android学习之路3:Activity布局

  大米的安卓手机中装了很多软件,各种游戏,各种软件,到最后发现其实都是由一个个小组件排版组成的,所以打算先研究一下Android的排版,并且在网上找了个介绍android例子,在研究彻底后,在这个例子的基础上加了一些自己的元素进去,demo请见文章最下面

  android的排版格式大致有这么几种:FrameLayout、LinearLayout、RelativeLayout、TableLayout。当然就跟web中的网页布局一样,可以多个一起使用。下面的例子主要介绍的排版内容有:
  1,LinearLayout布局

  2,FrameLayout布局

  3,RelativeLayout布局

  4,LinearLayout和RelativeLayout混合布局

  5,TableLayout布局

 

  首先看下整个demo的项目文件结构(主要部分用红色线框圈出),如下图:

 
                                
1,LinearLayout布局

  LinearLayout是比较常用的布局方式,它讲包含在内的子元素按照一个方向进行排列,方向可以使水平或者竖直方向(Android:orientation="vertival" or Android:orientation="horizontal")。
  例子中程序的主窗体就是用LinearLayout进行布局的,并且方向是竖行向下:
  
  再来看下代码(红色部分为关键代码):
  
<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:id="@+id/button0" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="FrameLayout的布局"></Button>
。。。。由于代码太长中间省略 。。。。。。。。。。。
<Button android:id="@+id/button4" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TableLayout布局"></Button>

</LinearLayout>
注:layout_width,layout_height:表示容器的长宽(可以直接写数字)
   wrap_content:表示容器只要能包裹住里面的view就行了
   fill_parent:意味着和父容器一下大
 
2,FrameLayout布局

  FrameLayout布局就好比一块屏幕上提前预定好的空白区域,然后可以填充元素到里面,比如一张图片,需要注意的是,所有的元素都放在
左上角,无法指定一个确切的位置。请看主要代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 注:
layout_width,layout_height:表示容器的长宽(可以直接写数字)
wrap_content:表示容器只要能包裹住里面的view就行了
fill_parent:意味着和父容器一下大
-->
<!-- 添加图片组件-->
<ImageView android:id="@+id/photo"
android:src="@drawable/bg"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
></ImageView>
</FrameLayout>
  
3,RelativeLayout布局

  即相对布局,容器里面的元素,如Button按钮等的位置是按照相对位置来计算的,例如例子中的两个button(确定和取消按钮):
当我们排版好第”取消按钮“后,我们可以定义“确定按钮“在“取消按钮”的上面、下面、左面或者右面。也就是说第二个button(确定按钮)在什么位置是依赖于第一个按钮(取消按钮)的位置,下面请看关键代码:
<EditText android:id="@+id/entry" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"
></EditText>
<!--
取消按钮和容器的右边齐平,并且设置左边的边距为10dip
-->
<Button android:id="@+id/cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip" android:text="取消"
></Button>

<!--
确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平
-->

<Button android:id="@+id/ok" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancel" --表明此按钮放在id为cancel的按钮左边
android:layout_alignTop="@id/cancel" --表明此按钮与id为cancel的按钮平行
android:text="确定"
></Button>

4,LinearLayout和RelativeLayout的混合布局

  最常用的两种布局方式混合使用后,会创建更加灵活多变的表单哦!如下图,是一个很简单的布局,最外层是LinearLayout,然后让其里面的子元素(里面的每个子元素都是RelativeLayout布局的)横向排布:
demo中我用两种不同的方式实现了这种混合布局效果,
第一种方法是直接把布局要求放进xml的模板布局文件中。
第二种方法是运行时创建模板,然后在放入Activity中去。
 
第一种方式(linearandrelativelayout.xml):
<?xml version="1.0" encoding="utf-8"?>
<!-- android:orientation:vertical(竖行排列) horizontal(横向排列) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
/*******************第一组*****************/
<RelativeLayout
android:id="@+id/left"
android:layout_width="100dp"
android:layout_height="100dp"
>
<TextView android:id="@+id/view1_1"
android:layout_width="fill_parent"
android:layout_height="50px"
android:text="第一组第一个"
android:background="@drawable/blue"
/>
<TextView android:id="@+id/view1_2"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_below="@id/view1_1"
android:text="第一组第二个"
android:background="@drawable/yellow" />
</RelativeLayout>
  /*******************第一组结束*****************/

/************************* 第二组 ******************/
<RelativeLayout
android:id="@+id/right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/right_view1"
android:background="@drawable/yellow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二组第一项" />
<TextView android:id="@+id/right_view2"
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/right_view1"
android:text="第二组第二项" />

</RelativeLayout>
 /*************************  第二组结束  ******************/

</LinearLayout>
第二种方式(LinearAndRelativeLayoutByRunTime.java):
package LayoutDemo.com;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class LinearAndRelativeLayoutByRunTime extends Activity {

public void onCreate(Bundle st){
super.onCreate(st);

//正对于这个Activity我们不指定res>layout目录下面的任何一个xml模板文件,而是在运行时创建一个模板
LinearLayout ll=new LinearLayout(this);//注意其中的参数,笔者刚开始也不是很理解,后台通过查看sdk,这个参数
//是android.content.Context类型,也就是这个Activity的上下文关系
//设定这个布局的排列方向(水平)
ll.setOrientation(LinearLayout.HORIZONTAL);

//把这个模板加入到这个Activity中去
setContentView(ll);

//LayoutInflater这个类是用来对xml模板布局文件进行解析,因为我们需要解析left.xml和right.xml两个文件
//并把这两个文件放到另一个模板布局文件中去
LayoutInflater flater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//实例化两个相对布局的模板文件
RelativeLayout layoutLeft = (RelativeLayout) flater.inflate(
R.layout.left, null);
RelativeLayout layoutRight = (RelativeLayout) flater.inflate(
R.layout.right, null);

//left.xml放入ll中去
ll.addView(layoutLeft,100,100);

//right.xml放入ll中去,用的方法跟left.xml是不一样的,使用了LayoutParmas类
//LayoutParmas顾名思义,就是布局参数类
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
ll.addView(layoutRight,lp);

}
}
5,TableLayout布局
  表格式的布局,作用是能使整个窗体看起来更加整齐有致,如图所示:
  
  具体的代码请看(framelayout.xml):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1">

<TableRow> --第一行
<TextView android:text="用户名:" android:textStyle="bold"
android:gravity="right" android:padding="3dip" />

<EditText android:id="@+id/username" android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>

<TableRow> --第二行
<TextView android:text="登录密码:" android:textStyle="bold"
android:gravity="right" android:padding="3dip" />

<EditText android:id="@+id/password" android:password="true"
android:padding="3dip" android:scrollHorizontally="true" />
</TableRow>

<TableRow android:gravity="right"> --第三行

<Button android:id="@+id/cancel"
android:text="取消" />

<Button android:id="@+id/login"
android:text="登录" />
</TableRow>
</TableLayout>
 
 
 代码(Demo):LayoutDemo
posted on 2012-01-20 00:07  南阿弥  阅读(519)  评论(0编辑  收藏  举报