Activity的作用:一个Activity相当于一个容器,用于存放各种控件的容器,也可以理解为是与用户交互的接口

创建Activity的要点:
   1.一个Activity就是一个类,并且这个类要继承Activity
   2.需要复写onCreate方法
   3。设置这个Activity所使用的布局文件(一般一个Activity对应一个布局文件,也可以多个Activity对应一个布局文件)
   4.每一个Activity都需要在AndroidManifest.xml文件当中进行注册
   5.在布局文件中为Activity添加必要的控件
   6.可以在Activity.java文件中通过控件的id找到需要的控件

Activity启动的基本流程:

  1.操作系统读取AndroidManifest.xml文件决定要运行哪个Activity,
  2.找到Activity之后操作系统自动生成Activity对象,
  3.系统自动调用Oncreate方法,在这个方法中读取指定的布局文件,显示布局文件里设置的内容

View:所有控件的父类,文本类,按钮类,布局,等等。。。。

 import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity {
//声明控件
private TextView testView;
private Button button;
int count = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置Activity使用的布局文件
setContentView(R.layout.activity_main);
//findViewById方法返回的View,所以要向下转型
testView = (TextView) findViewById(R.id.testView);
//这里设置控件属性和在布局文件设置效果是一样的,如果有同样的属性结果是显示这里设置的为准
testView.setText("huangjianfeng");
//设置这个控件的背景颜色
testView.setBackgroundColor(Color.BLACK); button = (Button) findViewById(R.id.button);
button.setBackgroundColor(Color.BLUE);
ButtonListener buttonListener=new ButtonListener();
button.setOnClickListener(buttonListener);
} class ButtonListener implements OnClickListener{ public void onClick(View v) {
count++;
testView.setText(count+"");
} } }

布局文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/testView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="0" /> <Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFF00"
android:text="0"
/> </LinearLayout>
<!--TextView:文本控件,
Button:按钮控件
match_parent:表示覆盖父控件-->

清单文件:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mars.viewdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mars.viewdemo.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>
</application> </manifest>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">ViewDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> </resources>

Activity与view的更多相关文章

  1. Activity Window View的关系

    http://blog.csdn.net/chiuan/article/details/7062215 http://blog.163.com/fenglang_2006/blog/static/13 ...

  2. Activity和View的区别:

    Activity和View的区别: activity相当于控制部分,view相当于显示部分.两者之间是多对多的关系,所有东西必须用view来显示.  viewGroup继承自view,实现了ViewM ...

  3. Activity Window View WindowManager关系&Touch事件分发机制

    http://www.cnblogs.com/linjzong/p/4191891.html https://www.cnblogs.com/kest/p/5141817.html https://b ...

  4. Activity转换为View和把图片转换为View

    package com.example.viewpager01; import java.util.ArrayList; import java.util.List; import android.a ...

  5. 底层剖析 Window 、Activity、 View 三者关系

    不管工作几年的 Android 工程师,或多或少都听说过 Window 的概念,并且隐隐约约感觉它在 Activity 与 View 之间应该发挥着某种连接的作用.但是如果需要说出这 3 者之间的关系 ...

  6. Android学习笔记1:Activity与View

    推荐一系列优秀的Android开发源码 Activity是Android应用中负责与用户交互的组件. View组件是所有UI控件.容器控件的基类,View组件就是Android应用中用户实实在在看到的 ...

  7. Android addView动态给Activity添加View组件

    本文主要讲述如何动态给UI界面添加布局和控件,在编程的时候很多时候需要动态显示一些内容,在动态添加View的时候,主要使用addView方法. 1. addView方法简介 在Android 中,可以 ...

  8. activity window view 关系

    1.Activity , Window和View的关系是什么? 跟踪Activity的源码就会发现:Activity.attch() -> PolicyManager -> Policy ...

  9. android中activity,window,view之间的关系

    activity:控制单元 window:承载模型 view:显示视图 几个小tip: 1.一个 Activity 构造的时候一定会构造一个 Window(PhoneWindow),并且只有一个 2. ...

随机推荐

  1. Haproxy+Heartbeat 高可用集群方案操作记录

    之前详细介绍了haproxy的基础知识点, 下面记录下Haproxy+Heartbeat高可用web集群方案实现过程, 以加深理解. 架构草图如下: 1) 基本环境准备 (centos6.9系统) 1 ...

  2. Python 判断文件/目录是否存在

    使用 os 模块 判断文件是否存在 os.path.isfile(path) 判断目录是否存在 os.path.isdir(path) 判断路径是否存在 # 使用 path 模块 os.path.ex ...

  3. 打印小票,使用的是BarcodeLib

    打印 private void Control_Click(object s,EventArgs e) { if (((Control)s).Name == "button1") ...

  4. c# 封装Dapper操作类

    using Dapper; using DapperExtensions; using System.Collections.Generic; using System.Configuration; ...

  5. Canvas-line.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. api接口签名认证的一种方式

    请求方 try { using (var client = new HttpClient()) { StringContent content = new StringContent(strParam ...

  7. Salesforce自定义权限简介

    自定义权限(Custom Permission) Salesforce默认提供了多种方式设定用户的权限,比如简档.权限集等.在这些设定中,已经包括了系统中的对象.应用.字段.页面布局等组件,管理员或开 ...

  8. 自定义控件详解(四):Paint 画笔路径效果

    Paint  画笔 ,即用来绘制图形的"笔" 前面我们知道了Paint的一些基本用法: paint.setAntiAlias(true);//抗锯齿功能 paint.setColo ...

  9. 《Inside C#》笔记(六) 属性、数组、索引器

    一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. ...

  10. Session Cookies随笔

    1.对于Session而言 它是用来保存在服务端的信息,可以用来做登录验证,在后台保存有用信息. 一个浏览器访问一个网站,就是建立一个连接,连接信息是独立的,就是在该建立的连接内,保存到Session ...