In the book, we create a new Android project and go to the Android Studio development page, such as:
The Project tool window on the left, which allows you to manage the relevant files for the entire project through the Project tools window
On the right is the code editing area where we can do the Android program development work
The bottom of the debug zone, in the debugging area can be a program breakpoint debugging, log printing, you can monitor the entire program memory consumption, so that targeted performance optimization.
First, by looking at the Project tool window area on the left, we introduce the basic structure of an Android project:
As shown, here we only need to focus on the contents of the app directory in the diagram:
Build directory: The building directory, equivalent to the Java SE Development bin directory, which is automatically generated, we do not need to care about the content, there is no need to change the content.
Libs Directory: Dependent package directory, we typically place third-party jar packages in this directory
SRC directory: src directory is a directory that we often deal with in our daily development, where the Androidtest directory and the test directory are used for testing, not here, we focus on the main directory, the main directory is divided into:java directory, res directory, Androidmanifest.xml file .
???? Java directory: storing Java code
???? Res directory: Store resource files, where:
???? drawable Storing picture Resources
???? Layout file
???? Mipmap is also the storage of image resources, in use and drawable no difference, but in terms of scaling to do some optimization, so we usually put the icon in the mipmap.
???? Value holds some string resources, which we'll cover in more detail later in this chapter.
???? Androidmanifest file: is a configuration file for the entire Android project
Run the code and get the result:
As you can see, the "Hello World" string is displayed on the phone
To build a house, for example, we must first have a drawing, that is, what kind of house we want to cover;
Then, we have to build the house according to the drawings, of course, we also have to register for the house in the "relevant departments."
In fact the Android program runs and covers the house the flow is similar.
First, look at the XML layout file code: Activty_main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:id= "@+id/activity_main"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent"7 Android:paddingbottom= "@dimen/activity_vertical_margin"8 Android:paddingleft= "@dimen/activity_horizontal_margin"9 Android:paddingright= "@dimen/activity_horizontal_margin"Ten Android:paddingtop= "@dimen/activity_vertical_margin" One Tools:context= "Com.rongma.helloworld.MainActivity"> A - <TextView - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" - Android:text= "Hello world!" /> - </Relativelayout>
Without having to know the meaning of this layout file code, we just need to know that this file is used to "tell the machine" and we want to display the "Hello World" string on the phone. (analogy to the house, in fact this is the drawing)
Then, look at the Java code: Mainactivity.java
1 Public class extends Activity {2 @Override3 protected void OnCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate); 5 Setcontentview (r.layout.activity_main); 6 }7 }
Not everyone can build a house, to understand the ability to lock "house", we want to let mainactivity inherit from the activity class, that is, all classes that describe a separate page inherit from the Activity class (or the activity's descendant class) in order to get "house" skills.
We can then see that in the OnCreate method, the Setcontentview method is called to get the layout. The parameter passed in is R.layout.activity_main. This method is used to obtain our "drawings" and to cover the house according to "drawings" and to operate the house. Well, the drawing here is R.layout.activity_main.
The R.java file is an Android app dictionary that is automatically generated from the application's resource files, where we can find maps related to Android layout resources, image resources, string resources, and so on. Here, R.layout.activity_main is actually the equivalent of calling the Activity_main.xml layout file under the Layout resource folder
Finally, the "house" should be registered to the "relevant departments":
The "relevant department" of the Android program------Androidmanifest.xml
The androidmanifest is called "related department" because it is a global profile for the entire Android app, which explains the app's name, icon, permissions, components, and so on.
This is the initial state of the system-generated androidmanifest.xml manifest file:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.rongma.helloworld">4 5 <Application6 Android:allowbackup= "true"7 Android:icon= "@mipmap/ic_launcher"8 Android:label= "@string/app_name"9 Android:supportsrtl= "true"Ten Android:theme= "@style/apptheme"> One <ActivityAndroid:name=". Mainactivity "> A <Intent-filter> - <ActionAndroid:name= "Android.intent.action.MAIN" /> - <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> the </Intent-filter> - </Activity> - </Application> - </Manifest>
In the Activity node, we can see Android:name= ". Mainactivity "Here corresponds to the com.rongma.helloworld.MainActivity file. That is, the mainactivity file was registered.
After each page is newly created, it needs to be registered under the manifest file, registering the method:
<android:name= "Activity filename"></activity >
If this page is "home", that is, the first page that appears when you just enter the app, its registration method:
<ActivityAndroid:name= "Activity's filename"><Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter></Activity>
Summary:
Let's show "Hello World" on the interface:
First, to write an XML layout file (drawing) under the Res/layout folder
Then, create a new class, Inherit from activity (unlock build skills), get layout file by Setcontentview method (get Drawing)
Finally, under the Androidmanifest.xml file, register (register with the relevant department)
§1.2 Android Project structure and "Hello World" Application analysis