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

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

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

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.luoye.layout"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.luoye.layout.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity //LinearLayout布局的Activity
android:name="com.luoye.layout.Linear"
android:label="Linear" >
</activity> <activity //RelativeLayout布局的Activity
android:name="com.luoye.layout.Relative"
android:label="Relative" >
</activity> </application> </manifest>

主Acitvity的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout Show" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout"
android:onClick="onClickListener"
/> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RelativeLayout"
android:onClick="onClickListener"
/> </LinearLayout>

线性布局的布局文件:

<?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:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#FF0000"
/> <TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#0000FF"
/> <TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#FFFF00"
/> <TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#008000"
/> </LinearLayout>

相对布局的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/tv1"
android:text="Type here:"
android:paddingLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <EditText
android:id="@+id/et1"
android:layout_below="@id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
/> <Button
android:id="@+id/ok"
android:layout_below="@id/et1"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:text="ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <Button
android:id="@+id/cancel"
android:layout_toLeftOf="@id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/ok"
android:text="cancel"
/> </RelativeLayout>

MainActivity.java

package com.luoye.layout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//实现Button的onClick事件,在布局文件中Button元素中声明了
public void onClickListener(View v)
{
Intent intent = new Intent();
switch(v.getId()) //按键来源判断
{
case R.id.button1:
intent.setClass(this, Linear.class);
break;
case R.id.button2:
intent.setClass(this, Relative.class);
break;
}
startActivity(intent); //启动对应布局的新的Activity
}
}

Linear.java

package com.luoye.layout;

import android.app.Activity;
import android.os.Bundle; public class Linear extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
}
}

Relative.java

package com.luoye.layout;

import android.app.Activity;
import android.os.Bundle; public class Relative extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relative);
}
}

主面板效果:

点击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. SSH深度历险记(两) Jboss+EJB一审

    学习感悟:每次学习新知识.通过初审会从小事做起,获得成就感.经典Hello Workd我们成功的例子奠定了门哈,呢.非常好的理解了.Ejb的核心. 今天主要以这个小实例,来学习EJB的分布式,我们能够 ...

  2. Eclipse生成jsp 如何将GB18030 改成默认UTF-8

    前两天面试被问到了struts的问题,好久没用了准备复习下,用eclipse创建一个maven项目的时候发现创建的jsp文件都是GB18030编码的,如何更改为UTF-8呢,其实很简单,给各位分享一下 ...

  3. 实现一个简单的Unity3D三皮卡——3D Picking (1)

    3D Picking 其原理是从摄像机位置到空间发射的射线.基于光线碰到物体回暖. 这里我们使用了触摸屏拿起触摸,鼠标选择相同的原理,仅仅是可选API不同. 从unity3D官网Manual里找到下面 ...

  4. Java 设计模式 -- 示例指南

    设计模式在软件开发者中非常受欢迎的.每个设计模式都是对常见软件问题的通用的描述解决方案. 我们使用设计模式的好处有: 1.设计模式已经对于一个重复出现的问题进行了定义并且提供了工业标准的解决方案,因为 ...

  5. GIMP也疯狂之动态图的制作(二)

    首先看下效果: (素材丢失,无法提供) 所用工具:GIMP.GIMP-GAP(在源中直接搜索安装) 文后会添加一个从U2B上搬运过来的视频教程,效果不错,值得一看本想也制作个人物变换,但几次实验,相同 ...

  6. 编写高效的jQuery代码

    http://www.css88.com/jqapi-1.9/ 编写高效的jQuery代码 最近写了很多的js,虽然效果都实现了,但是总感觉自己写的js在性能上还能有很大的提升.本文我计划总结一些网上 ...

  7. web后端server优化

    1,1. 就不需要优化一个页面模板,这些都是一些非常成熟的技术,甚至没有打招呼easy了10%的性能.这10%在整个页面的运行过程中仅仅占了0.5%的比例.微乎其微,等于是前面样例中的4车道变8车道的 ...

  8. Android 发展 ------------- Unable to resolve target &#39;android-19&#39;

    又一次装完Ecplise+ATD+Android SDK 在Ecplise工作空间导入之前写过的Android项目会出现错误,大部分是SDK 版本号不符,例如以下错误提示: Error:Unable ...

  9. 【C#版本详情回顾】C#3.0主要功能列表

    隐式类型的本地变量和数组 在与本地变量一起使用时,var 关键字指示编译器根据初始化语句右侧的表达式推断变量或数组元素的类型 对象初始值设定项 支持无需显式调用构造函数即可进行对象初始化 集合初始值设 ...

  10. MVC 在控制器中获取某个视图动态的HTML代码

    ASP.NET MVC 在控制器中获取某个视图动态的HTML代码   如果我们需要动态的用AJAX从服务器端获取HTML代码,拼接字符串是一种不好的方式,所以我们将HTML代码写在cshtml文件中, ...