[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 ...
随机推荐
- Jquery页面中添加键盘按键事件,如ESC事件
$(document).keydown(function(event){ if(event.keyCode == 38 || event.keyCode == 104){ i--; if(i<= ...
- Silverlight 雷达图和一种特殊泡泡画法
原文:Silverlight 雷达图和一种特殊泡泡画法 自上次发了雷达图,也没怎么说一下. 这次又做了一种图,继续共享一下,就是以一个点为中心,周围绕着几个点,用一个箭头与中心相连并带有某些信息.圆 ...
- PHP激活用户注册验证邮箱
本文将结合实例介绍如何使用PHP+Mysql完成注册帐号.发送激活邮件.验证激活帐号.处理URL链接过期的功能. 注册邮箱激活流程 <ul class='ul_demo''> <li ...
- 快速构建Windows 8风格应用23-App Bar概述及使用规范
原文:快速构建Windows 8风格应用23-App Bar概述及使用规范 本篇博文主要介绍App Bar概述.App Bar命令组织步骤.App Bar最佳实践. App Bar概述 Windo ...
- MVC 分页1 标准的url分页
一. 将mvcpager ddl 引用到web服务项目中. 二. 在view加入 <%@ Import Namespace="Webdiyer.WebControls.Mvc" ...
- Linux的错误码
在使用时需要包含头文件 #include <errno.h> merlin@tfAnalysis:~/projects/tfradius$ cat /usr/include/asm-gen ...
- [译]Java中的继承 VS 组合
(文章翻译自Inheritance vs. Composition in Java) 这篇文章阐述了Java中继承和组合的概念.它首先给出了一个继承的例子然后指出怎么通过组合来提高继承的设计.最后总结 ...
- 大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法)
原文:(原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法) 本篇文章主要是继续上一篇Microsoft决策树分析算法后,采用另外一种分析算法对目标顾客群体的挖掘 ...
- javascript面向对象2
原文:javascript面向对象2 首先我们先创建一个对象 var user = Object(); user.name = "张三"; user.age = 20; user. ...
- 使用pager进行分页
pager jar网址:http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm package com.binary.entity; impor ...