Android开发-目录结构详解
前言
学习Android开发,第一步就是要了解其目录结构,了解了其目录结构才可以快速进行开发,减少不必要的错误产生
图表总览
以下列出的是最常用的文件夹:
清单文件(AndroidManifest)
清单文件主要用于设置应用名称、图标、主题和注册四大组件(Activity、BroadcastReceive、Service和ContentProvider)
全局及Activity属性
Activity(活动) 是一个Android的应用组件,它提供屏幕进行交互。每个Activity都会获得一个用于绘制其用户界面的窗口,窗口可以充满哦屏幕也可以小于屏幕并浮动在其他窗口之上
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
/*设置全局属性*/
android:allowBackup="true" //是否允许备份
android:icon="@mipmap/ic_launcher" //应用图标
android:label="@string/app_name" //应用名称
android:screenOrientation="指定什么方向"; //指定什么方向
android:configChanges="横竖屏切换的设置"; //横竖屏切换的设置 {keyboardHidden|screenSize|orientation 屏幕大小.方向,软键盘发生变化都不会影响Activity的生命周期}
android:icon="应用程序的图标的地址" //应用程序的图标的地址
android:launchMode="启动模式" //启动模式
android:screenOrientation="屏幕的方向" //”landscape”(横屏,portrait是竖屏)
android:configChanges="keyboardHidden|orientation|screenSize" //当键盘隐藏、方向或大小发生改变的时候,调用activity的onConfigurationChanged方法保存activity状态
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">//主题类型
/*设置活动属性,app启动活动是依次向下的*/
<activity android:name=".MainActivity"> //活动名称
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
/*注册新的组件在这里*/
</application>
</manifest>
BroadcastReceive
BroadcastReceive(广播) 是一种广泛运用的在应用程序之间传输信息的机制,分静态注册和动态注册,一般在清单文件里注册属于静态注册,在Android 8中被不建议使用
<receiver android:name="包名"> //包类路径
<intent-filter android:priority="1000"> //意图过滤器,和优先级属性(-1000-1000)
<action android:name="接受的广播动作" /> //接受的广播动作
<data android:动作属性="数据参数类型"></data> //数据
</intent-filter>
</receiver>
一:数据的属性`在这里插入代码片`
scheme //(数据前缀)
host //(主机名) `在这里插入代码片`
mimeType //(数据的格式)
二:接受那个广播的动作(意图过滤器)"
<action android:name="android.intent.action.BOOT_COMPLETED"/> //开启启动完毕的事件
<action android:name="android.provider.Telephony.SMS_RECEIVED"/> //短信接受者(这个必须手打)
NEW_OUTGOING_CALL 新的电话向外拨打
PACKAGE_ADDEN 软件安装了
PACKAGE_REMOVED 软件卸载了
SCREEN_OFF 屏幕锁定
SCREEN_ON 屏幕解锁
ACTION_MEDIA_MOUNTED SD被挂载了的意图
Service
Service(服务) 是一个后台运行的组件,执行连续运行且不需要用户交互的任务,甚至应用被销毁也依然可以工作。
<service android:name="包类路径">可以有意图过滤器</service>
ContentProvider
ContentProvider(内容提供者组件) 通过请求从一个应用程序向其他应用程序提供数据。
<provider
android:authorities="主机名" //主机名可以随便起
android:name="aaa"> //包类名
</provider>
Java
在这里创建Java文件,如在MainActivity编写Java代码,实现APP的功能
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
绘图资源(Drawable)
Drawable 全称可绘制图像资源,用于存放你的绘图资源,例子如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp" />
<size
android:height="30dp"
android:width="20dp" />
<gradient
android:startColor="#9933cc"
android:endColor="#aa66cc"
android:angle="90"
/>
<padding android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="5dp"/>
</shape>
布局资源(Layout)
Layout 文件夹,是用来存放你绘制的布局资源,常用五种布局方式,分别是:FrameLayout(框架布局),LinearLayout(线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局)TableLayout(表格布局),详解点击此处,文件格式如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
图像资源(Mipmap)
Mipmap 用于存放常用的图像资源,如app的图标,back图片等,分别由mipmap-lhdpi、mipmap-mdpi、mipmap-hdpi、mipmap-xhdpi、mipmap-xxhdpi和mipmap-xxxhdpi这几个文件夹组成
mipmap-lhdpi | mipmap-mdpi | mipmap-hdpi | mipmap-xhdpi | mipmap-xxhdpi | mipmap-xxxhdpi | |
---|---|---|---|---|---|---|
DPI 范围 | (0-120]dpi | (120-160]dpi | (120-160]dpi | (240-320]dpi | (320-480]dpi | (480-640]dpi |
1DP对应px(luffy) | 0.75 | 1.0 | 1.5 | 2.0 | 3.0 | 4.0 |
键值资源(Values)
Values 用于存放键值对格式的资源,分别由Colors(颜色属性)、Strings(字符属性)和Styles(样式属性)三个文件组成
Colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
Strings
<resources>
<string name="app_name">My Application</string>
</resources>
Styles
<resources>
<!-- Base application theme. -->
<!--更改parent为Theme.AppCompat.Light.NoActionBar,可以去掉菜单栏-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
构建仓库(build.gradle: Project)
build.gradle: Project 用于添加Maven仓库和指定构建器版本
buildscript {
repositories {
google()
jcenter()
//新增的maven仓库地址
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0-rc03'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
构建模型(build.gradle: Module)
build.gradle: Module用于添加依赖和指定编译的SDK等级
apply plugin: 'com.android.application'
android {
//此处修改编译的SDK等级
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.taobao"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//此处添加依赖
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation 'com.squareup.retrofit2:retrofit:2.6.3'
implementation 'com.squareup.retrofit2:converter-gson:2.7.0'
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
implementation 'com.lcodecorex:tkrefreshlayout:1.0.7'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
//基础工具库
implementation "com.github.tamsiree.RxTool:RxKit:v2.4.1"
//UI库
implementation "com.github.tamsiree.RxTool:RxUI:v2.4.1"
//(依赖RxUI库时,需要额外依赖 cardview 库)
//noinspection GradleCompatible
implementation 'com.android.support:cardview-v7:27.1.1'
//功能库(Zxing扫描与生成二维码条形码 支付宝 微信)
implementation "com.github.tamsiree.RxTool:RxFeature:v2.4.1"
implementation 'com.google.zxing:android-core:3.3.0'
implementation 'com.google.zxing:core:3.3.2'
}