Android开发
文件详解
主要结构
业务处理
app—java
视图、资源
app—res
内部结构
MainActivity.java
主程序(入口)
app—java—MainActivity
文件中的activity标签 用于显示用户界面
- 用户通过activity 交互完成相关操作
- 一个app允许有多个activity
MainActivity.java 初始文件内容
package com.example.demo01;
import ...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
onCreate
onCreateOptionsMenu 选项菜单初始化
onOptionsItemSelected 菜单项被选中时触发事件
AndroidManifest.xml
清单文件
app—mannifests—AndroidManifest.xml
AndroidManifest.xml 初始文件内容
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo01">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
application
application :应用对象设置
- allowBackup :是否允许用户备份数据(默认为true)
- icon :app的图标
- roundIcon :app的圆形图标(用来适配不同系统主题下的应用图标)
- supportsRtl :是否支持Rtl(right-to-left 即从右到左大的布局)
- lable :app的名称
- theme :app的主题设置(主题背景颜色)
activity
activity 用于显示用户界面
- name= ". xx " : xx的注册页面
- label :显示页面的名字
- theme :显示页面的主题设置
- intent-filter : 过滤器 指定app的启动首页(首活动)
属性的值为@mipmap/ic_launcher
、@string/app_name
均为注释引入
实际为res
下的资源文件中的配置
我们可以Ctrl + 鼠标左键 进入文件查看
例如@string/app_name
为 app/res/values/strings.xml
生命周期
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
activities在系统中是以堆栈的形式来管理的。
每个activity有四个状态
- 活动中并可见,位于栈顶。
- 失去焦点、可见但未进行交互,位于栈的上端
- 停止或不可见但并未被销毁,位于栈的下端。
- 完成进行并销毁,出栈并记录状态,用户运行该activity,会重新启动后进程,并恢复销毁时的状态。
onCreate() 加载资源,程序开始运行时自执行,最高优先级。
onDestroy() 释放资源,程序运行完成或者被强制关闭时自执行。
onStart() 开始一个activity进程,使资源为用户可见
onStop() 结束一个activity进程,使资源为用户不可见
onResume() 恢复一个activity进程
onPause() 暂停一个activity进程
类继承关系
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity