Android 开发入门

文件结构

app

mainfests下面的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>//申请权限

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"//应用图标
        android:label="@string/app_name"//应用名称
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

java

res

res下面有以下几个文件夹:

  1. drwable

  2. layout:布局文件,java文件用来控制程序逻辑,layout用来外观设计

  3. mipmap

  4. values

  5. xml

2. layout 布局文件,
java文件用来控制程序逻辑,layout用来外观设计

4. values
包含3部分:

  • colors.xml:系统使用的所有颜色,都要在这里定义后才能使用

  • strings.xml:系统使用的所有String,都要在这里定义后才能使用

  • themes文件夹:主题样式:一般不修改

Gradle Scripts

包含:

  • build.gradle(Module:app)
  • gradle-wrapper.properties
  • gradle.properties
  • settings.gradle
  • local.properties

build.gradle(Module:app)

plugins {
    id 'com.android.application'
}

android {
    namespace 'cn.edu.sdut.myapplication'
    compileSdk 33

    defaultConfig {
        applicationId "cn.edu.sdut.myapplication"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

gradle-wrapper.properties
声明gradle版本,gradle的位置

#Fri Feb 24 17:21:58 CST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true

AndroidX包是最新的android的系统包

settings.gradle
配置下载资源的位置

local.properties
sdk的路径配置

资源引入

图片资源

图片资源分类

  • 应用图标资源:存放在mipmap文件夹中,直接在AndroidManifest.xml中引入。
  • 界面中使用的图片资源:存放在drawable文件夹中,需要先在dimen.xml中定义,然后在java中引入。

调用图片资源的方法
1.通过Java代码调用图片资源

//调用mipmap文件夹中资源文件
getResources().getDrawable(R.mipmap.ic_launcher);
//调用以drawable开头的文件夹中的资源文件
getResources().getDrawable(R.drawable.icon);

2.在XML布局文件中调用图片资源

@mipmap/ic_launcher   //调用mipmap文件夹中的资源文件
@drawable/icon            //调用以drawable开头的文件夹中的资源文件

主题和样式资源

主题是包含一种或多种的格式化属性集合,在程序中调用主题资源可改变窗体的样式,对整个应用或某个Activity存在全局性影响。
定义位置:res/values目录下的styles.xml文件中
标签

<style></style>:定义主题
<item></item>:设置主题的样式

布局资源

布局资源通常用于搭建程序中的各个界面。
定义位置:res/layout文件夹中

调用方式

  1. 通过Java代码调用布局资源文件
//在Activity的onCreate()方法中调用activity_main.xml布局资源
setContentView(R.layout.activity_main);
  1. 在XML布局文件中调用布局资源文件
//在XML布局文件中调用activity_main.xml布局资源
<include layout="@layout/activity_main"/>

字符串资源

定义位置:res/values/文件夹的strings.xml文件中
编写字符串资源内容

<resources>
    <string name="app_name">字符串</string>
</resources>

调用方式

  1. 通过Java代码调用字符串资源
在Activity的onCreate()方法中调用名为app_name的字符串资源
getResources().getString(R.string.app_name); 
  1. 在XML布局文件中调用字符串资源
//在XML布局文件中调用名为app_name字符串资源
@string/app_name

颜色资源

定义位置:res/values/文件夹的colors.xml文件中
colors.xml文件中的颜色资源

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

调用方式

  1. 通过Java代码调用颜色资源
//在Activity的onCreate()方法中调用名为colorPrimary的颜色资源
getResources().getColor(R.color.colorPrimary);
  1. 在XML布局文件中调用颜色资源
 <!-- 在XML布局文件中调用名为colorPrimary的颜色资源 -->
@color/colorPrimary

尺寸资源

尺寸资源:例如:View的宽高值

定义位置:res/values/文件夹的dimens.xml文件中
创建dimens.xml文件:
右键单击values文件夹,选中【New】  【XML】  【Values XML File】,在弹出框中输入dimens。

编写尺寸资源

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

调用方式

  1. 通过Java代码调用尺寸资源
在Activity的onCreate()方法中调用名为activity_horizontal_margin的颜色资源
getResources().getDimension(R.dimen.activity_horizontal_margin);
  1. 在XML布局文件中调用尺寸资源
 <!-- 在XML布局文件中调用名为activity_horizontal_margin的尺寸资源 -->
@dimen/activity_horizontal_margin

启用自适应图标

1.将图标文件设计好,存放在xx目录下

2.在 res 目录 右键-> New -> Image Asset
image

Image Asset功能图解
image

3.选择设计好的图标(前景和背景都要选择)
image
image

4.将图标引用改成 ImageAsset-Name栏的名字(默认为ic_launcher)

android:icon="@mipmap/ic_launcher"

修改启动类

  1. 新建启动类
    image
    只需要改个名字即可
    image

  2. 在AndroidManifest.xml修改

将新建的启动类的内部增加下面代码,然后exported改为true

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

更改后效果如下:

<!--        name的点 缺省的是项目的路径-->
        <activity
            android:name=".LoginActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
posted @ 2023-03-01 16:58  kingwzun  阅读(97)  评论(0编辑  收藏  举报