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-lhdpimipmap-mdpimipmap-hdpimipmap-xhdpimipmap-xxhdpimipmap-xxxhdpi
DPI 范围(0-120]dpi(120-160]dpi(120-160]dpi(240-320]dpi(320-480]dpi(480-640]dpi
1DP对应px(luffy)0.751.01.52.03.04.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'


}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知 不知

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值