I have always said that I want to learn java to learn android development, but I have been busy with it all the time. I started to learn it and it was interrupted. It is an excuse to say that you don't have time. Looking back at your life, there is a lack of passion, and there is no motivation to struggle, so it is a waste of time. I just took the GRE test and started learning about android development with a pleasant mood! Fight!
My tutorials are from 51CTO and posted here after reading, editing, and commenting. The original address of the tutorial is
Http://mobile.51cto.com/android-420819.htm -------------------------------------------------------------------------------------------------------------------------------------------
Introduction
This tutorial will mainly focus on exploration and understanding. However, the subsequent series of articles will further show you how to create a user interface, respond to user interaction operations, and use Java to orchestrate application logic. We will focus on the most common project content we encountered when we were just getting started with Android development, but it will also involve some other elements that already exist in the application structure. In today's article, we will not discuss these additional elements in depth. All in all, learning about the basic creation knowledge of Android applications is the teaching task we need to complete today.
1. Source
Step 1
Open Eclipse and search for the project we have created in Package Explorer. In"Src"Folder. The package should contain our Activity files, which must be opened in the editor. The source folder stores all the Java files we need to use when developing Android applications.
Every time we create a project,Java files. An application may have more than one package, and each package may contain multipleClass File. The processing code in these files can present our applications to users, respond to user interactions, and perform any necessary processing. In essence,Class files are Code related to applications based on the object-oriented conceptual model..
We will further discuss the concepts and practices of Java in subsequent articles. In today's tutorial, you only need to understand that a Java application splits various processing tasks into a certain number of objects. Each object is defined by a class declaration, which is usually an independent file in the application, but can also be nested in other class files.An object is basically a piece of code that carries a part of a function related to the application.Class FileThe code in can reference other classes in the application or other packages in the application.
When you start application development ,. A typical Android app that provides users with a user interface will have at least one Activity file, and more Activity classes will be used for different screen display content in the app. Other types of applications, such as tool programs or services, adopt different structures. You 'd better first followActivity UIThis type of application, and then access other application types.
Step 2
View the Activity files in the new application. We will further explore the Activity code in subsequent articles in this series of tutorials, so you do not need to pay too much attention to details. Today we are mainly targeting the main Activity in the application, which will take effect at the same time after the application is started. Your applications may also start other activities for user interaction. When we create our own project, Eclipse will set the application and use the main Activity as the main class-it will also be displayed as the main Activity in the project list, we will see it later.
In the main Activity class, you will seeOnCreate MethodThe code will be executed when the Activity is created, that is, when the application starts. In this method, you will see the following code lines:
SetContentView (R. layout. activity_main );
After we start the project, this line of content is used to specify the layout file we created and tell Android to use it as the content view. This means that no matter what content the layout file contains, the Activity will be displayed to the user when the Activity is displayed on the screen.
We will discuss related topics later. Currently, we need to focus on "R. layout. activity_main "."Syntax. This is how our Java code references application resources. Similar syntaxes can be used to reference resources such as instance slices and data values. Here is the layout. These resources must be identified based on their names. For the layout in the example, the file name is used. It is inferred that the syntax we want to use is "R. type. name ". After we start programming, you will start to use this syntax.
In subsequent articles of this series, we will add code to the Activity class file to implement user interaction. Now open the "res" folder in the application and you will find multiple subfolders in it. These folders are created by default by Eclipse and ADT after we enable the new Android project. However, we may need to add other directories for different types of resources.
2. layout ResourcesRes/layout
As we can see, the layout file generated after the project is created is saved in the "res/layout" folder. Multiple Activity screensGenerally, an independent layout file is retained for each screen. You may also use the layout file for individual UI entries. When you create a class file for the Activity, you need to use setContentView for layout settings as described above. In addition, you can use Java code to set the layout. This is an alternative. In our example, layout settings are dynamically generated during application execution. However, the advantage of XML is that we can intuitively feel the visual effect of the layout scheme in the interface design work.
In the main layout file of the application (which should be opened in the editor now), you will see the XML structure. If you have never touched XML before, you don't have to worry about it. We will further discuss this basic knowledge in subsequent articles. For the moment, you only need to understand that XML is a markup language, similar to HTML-if you have been familiar with Web development before. XML file ExploitationTree StructureAs a data model. Generally, a layout file has a root layout element and uses it as a model of a specific layout type.
3. resources that can be drawnDrawable
You can see multiple folders in the Resource Directory that contain the words "drawable" in their names. These folders are used to save the image files used by the application. These image files can be digital image files we have prepared outside of Eclipse, in the format of PNG or JPEG. Or,You can also use XML code to describe the shape, color, and appearance to define specific printable resources..
The Resource Directory will retainDrawable of the density bucket (density)Folder. These density bins are the basis for general classification of pixel density of devices running Android systems. Specific categories are dividedLow ld, medium md, high hd, Ultra High xhd, and ultra high xxhdDensity. With the help of density bins, we can easily simplify the multi-screen density support process by selecting the corresponding type. This means that when an image file is included in a project, it can be placed in folders of different density, and the version that meets various density schemes can be tailored.
4. Data Resource values
In the "res" directory, we will see some folders with the words "values" in the title. These folders are used to accommodateData Value. These values can containText strings and numbers. The folder containing values of the XML file lists one or more values.. Each list contains a name and a value in the content. Other files in the application, such as Java classes or layout files ,. In typical use cases, we can use these values stored in text strings to display content in the UI elements-such as buttons.
Different value files in the application allow you to modify the value for a specific screen size and API level. If the same value is sufficient for multiple devices, they can be directly saved in the "Values" folder.
5. inventory fileAndroidManifest. xml
When you view the main folder in the application, you will find the configuration file of the project. Double-click the editor to open it. Next, we will see a graphical interface showing its content. Click "AndroidManifest. xml" at the bottom of the editor window to view its XML code.This file defines all aspects of the application as a unified whole. Eclipse and ADT will create specific elements in the list while creating an application. The specific creation method depends on the settings in the project creation process. You can manually add other elements to the List, such as adding other activities.
We will run some of the main elements to understand the functions of the list, but there are other elements that can be included. In the new application project elements listed in the list, we can see that we use it to represent the minimum and target API levels. Contains attributes pointing to the startup Mechanism and Application name. There is alsoWhen the application starts running, it starts using the intent-filter element as the main Activity. When we add a new Activity to the application, a new activity element is added for each related element.
You may also need to add other elements to the List, including. Permissions include multiple operation entries, such as retrieving data through the Internet, writing data to storage, or accessing other features on the device-such as cameras. The List also lists the device types supported by applications and some other application components (such as background services ).
6. other files
Here, we have talked about the main aspects of the Android Application project structure that you need to understand. As you learn about Android development, you will often deal with this content in the future. Through Eclipse, we will also see some other files and directories contained in the project, but for now, we can basically ignore them directly.
As we can see earlier, you can use. Eclipse and the ADT of the management system will reference resources from Java in the application.
Prompt: When you start to develop Android applications, you may encounter problems when using R. If Eclipse displays any R-related error information, especially"R cannot be parsed as a variable.", You need to check the starting content of the class file to see if there is an" R "import Statement, such as" import android. R ;". If the corresponding content is found, especially after the code has been copied and pasted into the file, delete this import statement. If you encounter other R-related prompts, make sure there are no errors in the resource file. If the problem persists ,. When all efforts fail, try to restart Eclipse.
Summary
In today's article, we learned the basic knowledge about the Android project structure. You can view other files and folders in the project at will to understand the overall structure of the project. In the subsequent tutorial, we will create user interface elements in the application and process user interaction operations. We will also discuss the basic features of Java programming to further improve our understanding of Android development projects.
Link: http://mobile.tutsplus.com/tutorials/android/android-sdk-app-structure/