[Android笔记1]Activity+Layout+Button
线性布局(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的更多相关文章
- Android笔记——Button点击事件几种写法
Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- Android笔记---Intent实现Activity跳转
学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...
- Android笔记(五十八)Android总结:四大组件——Activity篇
什么是Activity Activity是一种包含用户界面的组件,主要用于和用户进行交互,一个APP通常由多个Activity组成. 每个Activity都对应一个布局文件,通过setContentV ...
- Android笔记(二十) Activity中的跳转和值传递
我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...
- Android笔记(五) Activity的启动模式
Android中Activity是由返回栈来管理的,在默认情况下,每当启动一个新的Activity,它都会在返回栈中入栈,并且出于栈的顶端.但是有些时候Activity已经在栈的顶端了,也就不需要再启 ...
- Android笔记(四) Activity之间的数据传递
我们之前使用Intent进行Activity之间的跳转,其实Intent还可以在启动活动的时候传递数据. Intent提供了一系列的putExtra方法以便我们把想要传递的数据暂存在Intent中,待 ...
- Android笔记(三) 使得Activity之间可以跳转---Intent
什么是Intent 一个APP肯定不单单由一个Activity构成,我们在使用过程中,经常需要在多个Activity中跳转,Android中Intent可以帮我们来完成在各个Activity中跳转的功 ...
- android开发学习笔记:圆角的Button
转自:http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html 在res目录下的drawable-mdpi建立xml文件shape.x ...
随机推荐
- Android 记录的(MediaRecorder)而播放(MediaPlayer)
经MediaRecorder和MediaPlayer实现声音记录和回放,代码比较简单,直接附着到代码. xml文档面对只有四个button不贴. UI watermark/2/text/aHR0cDo ...
- Linq的理论知识
概述 前面的博客中写到过关于Linq的一些知识,可是,没有具体的说Linq,本篇博客将会说一下Linq. 什么是Linq Linq是一个概念,它实现了数据查询使用同一方式,即,它使我们程序猿通过使用它 ...
- 快速构建Windows 8风格应用3-打包发布应用
原文:快速构建Windows 8风格应用3-打包发布应用 本篇博文主要介绍如何打包应用程序,成功后如何部署应用程序,最后介绍如何发布应用程序到应用商店中. 如何打包Windows 8风格应用程序呢? ...
- Linux分配给该用户没有权限登陆
Linux分配给该用户没有权限登陆 sudo visudo username ALL=(ALL) NOPASSWD:ALL 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- 数以百万计美元的融资YO是什么东东?
给自己做个广告哈,新栏目"面试"已经推出,回复"面试"就可以获取. 这两天最火的应用是什么.非yo莫属,堪称史上最简单的社交应用,仅仅能向好友发送一个yo. 出 ...
- Python 3语法小记(九) 异常 Exception
常见异常: Exception 所有异常的基类 AttributeError 特性应用或赋值失败时引发 IOError ...
- KNN算法的理解
一.算法 1.kNN算法又称为k近邻分类(k-nearest neighbor classification)算法. 最简单平庸的分类器或许是那种死记硬背式的分类器,记住全部的训练数据.对于新的数据则 ...
- Visual Studio 2013 的 Browser Link 功能
Visual Studio 2013 的 Browser Link 功能 最近公司弄新项目需要用 MVC,就把 IDE 升级到了 Visual Studio 2013,在开发的时候发现有好多请求一个本 ...
- .net微软消息队列(msmq)简单案例
1.首先我们需要安装消息队列服务,它是独立的消息记录的服务,并保存在硬盘文件中. 我们添加名为:DMImgUpload的私有消息队列. 2.定义消息队列的连接字符串建议采用IP: (1)FormatN ...
- 最初的ajax案例
----------------------原始的ajax: function guo() { var xhr if (XMLHttpRequest) { ...