App engineering structure

After a lot of hard work and various pit filling, Android Studio was finally installed, and the first APP (hello world) was successfully run on its own simulator. Through this APP, the basic engineering structure of APP was studied first, so that the later development was more handy.

First, go to the catalog structure of HelloWorld project

 

From the structure chart, we can see that there are two directories under the project: one is app, the other is Gradle Scripts.

Among them, app is divided into the following three subdirectories

(1) There is only one Android manifest.xml file, which is the configuration file of APP running

(2)java. There are two packages: com.example.animator.android'learning and com.example.animator.android'learning. They store the java source code of APP project and the java code for testing

(3)res is the resource file of APP project. There are four subdirectories under it

· drawable stores graphic description files and user pictures

· layout stores layout documents of APP pages

· mipmap stores the start Icon

· values stores some constant definition files, such as string.xml, pixel constant dimensions.xml, color colors.xml and style definition styles.xml

Under Gradle Scripts is the compilation configuration file of the project, where

(1)build.gradle is divided into project level and module level to describe the Compilation Rules of APP project

(2)proguard-rules.pro is used to describe the code obfuscation rules of java files

(3)gradle.properties is used to configure the command-line parameters of the compilation project, which generally does not need to be changed

(4)setting.gradles configures which modules are compiled together. The initial content is include ":app", which means that only app modules are compiled

(5) The local configuration of the local.properties project generally does not need to be changed. The modified file is generated automatically when the project is compiled. It is used to describe the developer's local environment configuration, such as the SDK's local path, NDK's local path, etc

 

About the configuration file build.gradle

The following text notes are added to the original build.gradle file to facilitate the understanding of the purpose of each parameter

apply plugin: 'com.android.application'

android {
    //Specify the SDK Version, such as 21 for use Android5.0 Compile
    compileSdkVersion 21
    //Specifies the version number of the compiler, where the first two digits must match the compileSdkVersion Keep consistent. The specific version number can be found in SDK Of the installation directory“ sdk\build-tools"Find below
    buildToolsVersion "21.1.1"

    defaultConfig {
        //Specify the application number of the module, i.e APP The package name of is changed to automatically generated without any change
        applicationId "com.example.animator.android_learning"
        //Appoint APP Minimum suitable for operation SDK Version number
        minSdkVersion 21
        //Specify the SDK Version number, that is APP Which version of Android Up operation
        targetSdkVersion 21
        //Appoint APP Application version number of
        versionCode 1
        //Appoint APP Application version name of
        versionName "1.0"
    }
    buildTypes {
        release {
            //Specifies whether to turn on code obfuscation
            minifyEnabled false
            //Specifies the file name of the code obfuscation rule file
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

//Appoint APP Compiled dependency information
dependencies {
    //Specified reference jar Package path
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //For unit test compilation Junit Version number
    testCompile 'junit:junit:4.12'
    //Specified compilation Android Higher version support library for
    compile 'com.android.support:appcompat-v7:21.0.2'
}

About APP running configuration Android manifest.xml

It is used to specify the internal operation configuration of the APP. It is an xml description file. The root node is the manifest, and the package of the root node specifies the package name of the APP. The description of the lower molecular node of the manifest is as follows:

(1) Uses permission is used to declare the permission name needed during APP operation, such as camera permission, location permission, etc

(2)application is used to specify the properties of APP itself. The default values are as follows:

· allow backup

· icon APP icon displayed on the mobile screen

· name of the label APP on the mobile screen

· supportsRtl is set to true to allow right to left text order

· theme specifies the display style of APP

 

Manipulating controls in code

First, you can see the content of the page layout file of helloworld project as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.animator.android_learning.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

It can be seen that the xml layout file is similar to HTML as the front-end. For example, the layout [width] is similar to the width attribute in HTML, the layout [height] is similar to the height attribute in HTML, and the TextView is similar to the textarea control in HTML. It can be seen that the layout configuration of APP is not difficult. It can be said that "I have development documents in hand".

The corresponding java code files are as follows

package com.example.animator.android_learning;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Through searching related materials and tutorials, we know that onCreate method fills the layout style of APP page by referencing the name of layout file. Just like JavaScript and HTML, java can also obtain the xml tag corresponding to ID through code (provided that ID android:id="@+id/helloworld" is added to the xml tag)

Simply rewrite TextView text with code as follows

package com.example.animator.android_learning;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView= (TextView) findViewById(R.id.helloworld);
        textView.setText("Xiaozhao's first operation!");
        textView.setTextColor(Color.RED);
        textView.setTextSize(30);

    }
}

The effect is as follows:

Tags: Android xml Gradle Java

Posted on Sun, 12 Jan 2020 03:48:39 -0800 by brcolow