线性布局(LinearLayout)是指view对象在父view中可按水平或垂直方向线性排列。

相对布局(RelativeLayout)是指view对象的排列依赖于各对象之间的相对位置。

下面是展示两者的小例子,同时展示如何启动一个新的Activity和监听Button按键事件的方式。

AndroidManifest.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.luoye.layout"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="17" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.luoye.layout.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24.  
  25. <activity //LinearLayout布局的Activity
  26. android:name="com.luoye.layout.Linear"
  27. android:label="Linear" >
  28. </activity>
  29.  
  30. <activity //RelativeLayout布局的Activity
  31. android:name="com.luoye.layout.Relative"
  32. android:label="Relative" >
  33. </activity>
  34.  
  35. </application>
  36.  
  37. </manifest>

主Acitvity的布局文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="Layout Show" />
  11.  
  12. <Button
  13. android:id="@+id/button1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="LinearLayout"
  17. android:onClick="onClickListener"
  18. />
  19.  
  20. <Button
  21. android:id="@+id/button2"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="RelativeLayout"
  25. android:onClick="onClickListener"
  26. />
  27.  
  28. </LinearLayout>

线性布局的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1"
  11. android:background="#FF0000"
  12. />
  13.  
  14. <TextView
  15. android:layout_width="fill_parent"
  16. android:layout_height="0dip"
  17. android:layout_weight="1"
  18. android:background="#0000FF"
  19. />
  20.  
  21. <TextView
  22. android:layout_width="fill_parent"
  23. android:layout_height="0dip"
  24. android:layout_weight="1"
  25. android:background="#FFFF00"
  26. />
  27.  
  28. <TextView
  29. android:layout_width="fill_parent"
  30. android:layout_height="0dip"
  31. android:layout_weight="1"
  32. android:background="#008000"
  33. />
  34.  
  35. </LinearLayout>

相对布局的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <TextView
  7. android:id="@+id/tv1"
  8. android:text="Type here:"
  9. android:paddingLeft="16dp"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. />
  13.  
  14. <EditText
  15. android:id="@+id/et1"
  16. android:layout_below="@id/tv1"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:paddingLeft="16dp"
  20. android:paddingRight="16dp"
  21. />
  22.  
  23. <Button
  24. android:id="@+id/ok"
  25. android:layout_below="@id/et1"
  26. android:layout_alignParentRight="true"
  27. android:layout_marginRight="16dp"
  28. android:text="ok"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. />
  32.  
  33. <Button
  34. android:id="@+id/cancel"
  35. android:layout_toLeftOf="@id/ok"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_alignTop="@id/ok"
  39. android:text="cancel"
  40. />
  41.  
  42. </RelativeLayout>

MainActivity.java

  1. package com.luoye.layout;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8.  
  9. public class MainActivity extends Activity {
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. }
  16.  
  17. @Override
  18. public boolean onCreateOptionsMenu(Menu menu) {
  19. // Inflate the menu; this adds items to the action bar if it is present.
  20. getMenuInflater().inflate(R.menu.main, menu);
  21. return true;
  22. }
  23. //实现Button的onClick事件,在布局文件中Button元素中声明了
  24. public void onClickListener(View v)
  25. {
  26. Intent intent = new Intent();
  27. switch(v.getId()) //按键来源判断
  28. {
  29. case R.id.button1:
  30. intent.setClass(this, Linear.class);
  31. break;
  32. case R.id.button2:
  33. intent.setClass(this, Relative.class);
  34. break;
  35. }
  36. startActivity(intent); //启动对应布局的新的Activity
  37. }
  38. }

Linear.java

  1. package com.luoye.layout;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class Linear extends Activity{
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.linear);
  12. }
  13. }

Relative.java

  1. package com.luoye.layout;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class Relative extends Activity{
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.relative);
  12. }
  13. }

主面板效果:

点击LinearLayout按钮:

点击RelativeLayout按钮:

[Android笔记1]Activity+Layout+Button的更多相关文章

  1. Android笔记——Button点击事件几种写法

    Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...

  2. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  3. Android笔记---Intent实现Activity跳转

    学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...

  4. Android笔记(五十八)Android总结:四大组件——Activity篇

    什么是Activity Activity是一种包含用户界面的组件,主要用于和用户进行交互,一个APP通常由多个Activity组成. 每个Activity都对应一个布局文件,通过setContentV ...

  5. Android笔记(二十) Activity中的跳转和值传递

    我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...

  6. Android笔记(五) Activity的启动模式

    Android中Activity是由返回栈来管理的,在默认情况下,每当启动一个新的Activity,它都会在返回栈中入栈,并且出于栈的顶端.但是有些时候Activity已经在栈的顶端了,也就不需要再启 ...

  7. Android笔记(四) Activity之间的数据传递

    我们之前使用Intent进行Activity之间的跳转,其实Intent还可以在启动活动的时候传递数据. Intent提供了一系列的putExtra方法以便我们把想要传递的数据暂存在Intent中,待 ...

  8. Android笔记(三) 使得Activity之间可以跳转---Intent

    什么是Intent 一个APP肯定不单单由一个Activity构成,我们在使用过程中,经常需要在多个Activity中跳转,Android中Intent可以帮我们来完成在各个Activity中跳转的功 ...

  9. android开发学习笔记:圆角的Button

    转自:http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html 在res目录下的drawable-mdpi建立xml文件shape.x ...

随机推荐

  1. Jquery页面中添加键盘按键事件,如ESC事件

    $(document).keydown(function(event){ if(event.keyCode == 38 || event.keyCode == 104){ i--; if(i<= ...

  2. Silverlight 雷达图和一种特殊泡泡画法

    原文:Silverlight 雷达图和一种特殊泡泡画法 自上次发了雷达图,也没怎么说一下. 这次又做了一种图,继续共享一下,就是以一个点为中心,周围绕着几个点,用一个箭头与中心相连并带有某些信息.圆 ...

  3. PHP激活用户注册验证邮箱

    本文将结合实例介绍如何使用PHP+Mysql完成注册帐号.发送激活邮件.验证激活帐号.处理URL链接过期的功能. 注册邮箱激活流程 <ul class='ul_demo''> <li ...

  4. 快速构建Windows 8风格应用23-App Bar概述及使用规范

    原文:快速构建Windows 8风格应用23-App Bar概述及使用规范 本篇博文主要介绍App Bar概述.App Bar命令组织步骤.App Bar最佳实践.   App Bar概述 Windo ...

  5. MVC 分页1 标准的url分页

    一. 将mvcpager ddl 引用到web服务项目中. 二. 在view加入 <%@ Import Namespace="Webdiyer.WebControls.Mvc" ...

  6. Linux的错误码

    在使用时需要包含头文件 #include <errno.h> merlin@tfAnalysis:~/projects/tfradius$ cat /usr/include/asm-gen ...

  7. [译]Java中的继承 VS 组合

    (文章翻译自Inheritance vs. Composition in Java) 这篇文章阐述了Java中继承和组合的概念.它首先给出了一个继承的例子然后指出怎么通过组合来提高继承的设计.最后总结 ...

  8. 大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法)

    原文:(原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法) 本篇文章主要是继续上一篇Microsoft决策树分析算法后,采用另外一种分析算法对目标顾客群体的挖掘 ...

  9. javascript面向对象2

    原文:javascript面向对象2 首先我们先创建一个对象 var user = Object(); user.name = "张三"; user.age = 20; user. ...

  10. 使用pager进行分页

    pager jar网址:http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm package com.binary.entity; impor ...