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'


}

發佈了17 篇原創文章 · 獲贊 3 · 訪問量 1358
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章