线性布局(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. Windows下一个ROracle安装与使用

    ROracle一个简短的引论: ROracle这是R连接到接入Oracle数据库DBI(Oracledatabase interface)介面.这是基于OCI一个DBI兼容Oracle司机. 具体见说 ...

  2. Codeforces 527C Glass Carving(Set)

    意甲冠军  片w*h玻璃  其n斯普利特倍  各事业部为垂直或水平  每个分割窗格区域的最大输出 用两个set存储每次分割的位置   就能够比較方便的把每次分割产生和消失的长宽存下来  每次分割后剩下 ...

  3. QT界面应用程序的语言国际化

    对字符串使用tr包含起来,因为tr是QObject的一个静态函数,所以它可以直接调用. 在生成language.ts文件之前应该编辑.pro文件,如下: QT += core gui greaterT ...

  4. 负载均衡DNS和反向代理优缺点

    负载均衡 (Load Balancing) 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. 负载均衡(又 ...

  5. FineUI开发实践

    ASP.NET-FineUI开发实践-7 摘要: 下拉显示grid列表.其实很简单,但是试了很多方法,水平有限,主要是都不好使,还是简单的好使了,分享下.先是看了看网上的,是直接写个了extjs控件类 ...

  6. 【转】Android Application 对象介绍

    What is Application Application和Activity,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application ...

  7. Javascript多线程引擎(三)

    Javascript多线程引擎(三) 完成对ECMAScript-262 3rd规范的阅读后, 列出了如下的限制条件 1. 去除正则表达式( 语法识别先不编写) 2. 去除对Function Decl ...

  8. java数字字符串累加1的解决方案

    近期操作项目遇到这样的问题,研究了下搞出了一个解决方案. //num也可以是在数字字符串里面截取的,比如我有14位的数字字符串前六位是市级,7,8位代表县区,后两位代表乡镇,最后四位是累计+1的,这个 ...

  9. Enumerable和yield

    说说IEnumerable和yield IEnumerable数据类型是我比较喜欢的数据类型,特别是其强类型IEnumerable<T>更获得Linq的支持使得代码看起来更加优雅.整洁. ...

  10. linux内核数据结构之链表

    linux内核数据结构之链表 1.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该 ...