Or go back to Eclipse, first expand the HelloWorld project, and you'll see the directory structure shown in 1.17.
Figure 1.17
You will feel a little dizzy when you start to see so many strange things. Don't worry, I'm going to talk about one by one of the content now, and you won't feel that much effort if you look at this picture soon.
1. src
There is no doubt that the SRC directory is where all of our Java code is placed, and its meaning here is exactly the same as the SRC directory under the ordinary Java project, and you will see that the helloworldactivity file we just created is inside.
2. Gen
The contents of this directory are automatically generated, there is a R.java file, and any resources you add to the project will generate a corresponding resource ID in it. This file should never be manually modified.
3. Assets
This directory is not used much, the main can be stored in a number of files packaged with the program, you can dynamically read the contents of these files when your program runs. In addition, if you use the WebView to load the local Web page, all the pages related files are also stored in this directory.
4. Bin
This directory you do not need to pay much attention to, it mainly contains some of the files generated automatically at compile time. There will be a compilation of your current project installed package, expand the bin directory you will see HELLOWORLD.APK, the file is copied to the phone can be installed directly.
5. Libs
If you use a third-party jar package in your project, you will need to place the jar packages in the Libs directory, and the jar packages placed in this directory will be automatically added to the build path. You can expand in Android 4.0,
Android Privatelibraries, Android Dependencies these libraries, which show the Jar packages are already
Added to the build path.
6. Res
The contents of this directory is a bit more, simply say, you use in the project all the pictures, layouts, strings and other resources to be stored in this directory, the earlier mentioned R.java content is based on the files in this directory automatically generated. Of course, there are many subdirectories under this directory, the images are placed in the drawable directory, the layout is placed in the layouts directory, the strings are placed in the values directory, so you do not have to worry about the entire res directory mess.
7. Androidmanifest.xml
This is the configuration file for your entire Android project, and all four components you define in the program need to be registered in this file. You can also add permission declarations to your application in this file, or you can reassign the minimum and target versions of the program that you specified when you created the project. Since this file will often be used later, we use the time to do a detailed explanation.
8. Project.Properties
This is a very simple file that specifies the version of the SDK that is used to compile the program through a single line of code. Our HelloWorld Project uses API 14, and you can change it here to another version and try it. So the entire project directory structure will be introduced, if you do not fully understand the words are normal, after all,
There are too many things you haven't touched. Don't worry, this will not affect the learning behind you. On the contrary, you will find it particularly clear and simple when you come back and look at the directory structure after you complete the book.
Next, let's analyze how the HelloWorld project works. First Open
Androidmanifest.xml file, from which you can find the following code:
<activityandroid:name= "com.test.helloworld.HelloWorldActivity" android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<categoryandroid:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This code indicates that helloworldactivity is registered for this activity, and no activity that is registered in Androidmanifest.xml is not available. The two lines of code in Intent-filter are very important, <action android:name= "Android.intent.action.MAIN"/> and <categoryandroid:name= " Android.intent.category.LAUNCHER "/> said Helloworldactivity is the main activity of the project, click on the app icon on the phone, the first thing to start is this activity.
What effect does that helloworldactivity concrete have? I said when I introduced the four components of Android,
Activity is the façade of the Android app, and everything you see in the app is placed in the event. So
The interface you see in Figure 1.15 is actually helloworldactivity this activity. So let's go and look at its code, open helloworldactivity, the code looks like this:
Public Classhelloworldactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.hello_world_layout);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; Thisadds items to the Actionbar if it is present.getmenuinflater (). Inflate (R.menu.hello_world, menu);
return true;
}
}
First we can see that helloworldactivity is inherited from the Activity. Activity is an active base class provided by the Android system, and all activities in our project must inherit it in order to have an active feature. Then you can see that there are two methods in Helloworldactivity, Oncreateoptionsmenu () This method is used to create the menu, we can ignore it first, mainly look at the OnCreate () method. The OnCreate () method is a method that must be executed when an activity is created, with only two lines of code and no word for the Hello world!. So where does the Hello world! shown in Figure 1.15 be defined?
In fact, the design of the Android program is logical and view separation, it is not recommended to write the interface directly in the activity, a more general way is to write the interface in the layout file, and then introduced in the activity. As you can see, the second line of the OnCreate () method calls the Setcontentview () method, which is the method that introduces a hello_world_layout layout to the current activity, and the Hello world! must be defined here! Let's open this file and take a look.
Layout files are defined in the Res/layout directory, and when you expand the layout directory, you will see
Hello_world_layout.xml this file. After opening, the code looks like this:
<relativelayout xmlns:android= " HT T P: / / S C He m as . an D ro I d. C om / AP k / R es / an D ro I D " 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= ". Helloworldactivity ">
<textviewandroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello _world "/>
</RelativeLayout>
Still can't read it? It's okay, I'll explain the layout in detail later, you just have to see a TextView in the code above, which is a control provided by the Android system for displaying text in the layout. Then you finally in the TextView see the word Hello World, haha finally found, the original is through the android:text= "@string/hello_world" This code definition! Hey? Feel something wrong ah, as shown in Figure 1.15 is helloworld!, this exclamation point why not, the case is not the same.
You're still being cheated, the real Hello world! string is not defined in the layout file. Android does not recommend hard-coding a string in a program, and a better practice is to define the string in Res/values/strings.xml, which can then be referenced in a layout file or code. Now let's open the Strings.xml and look at the contents as follows:
<resources>
<string name= "App_name" >HelloWorld</string>
<stringname= "Action_settings" >Settings</string>
<string name= "Hello_world" >Helloworld!</string>
</resources>
There is nothing to escape your discernment, Hello world! string is defined in this file. And the string definition is the use of key-value pairs, the Hello world! value corresponds to a key called Hello_world, so in the Hello_world_layout.xml layout file is by reference to the Hello_world this key, The corresponding value was found.
At this point, I accidentally glanced at this file. There is also a key called App_name. You guessed it, and we can change the name of the application here by modifying the value of the app_name. Where in the world did you cite the App_name key? Open the Androidmanifest.xml file and find out for yourself!
1.3.4 Resources in a detailed project
If you expand the Res directory to look at, in fact, there are still quite a lot of things, it is easy to see the dazzling, 1.18 shows.
Figure 1.18
See so many folders do not fear, in fact, summed up, res directory becomes very simple. All folders beginning with drawable are used to put pictures, all folders beginning with values are used to put strings, the layout folder is used to put the layouts file, the menu folder is used to put the menus file. How, does it suddenly feel a lot clearer? The reason why there are so many drawable opening folders, in fact, is mainly to allow the program to be compatible with more devices. In the production process, it is best to give the same picture a few different resolutions of the copy, placed in these folders, and then when the program is running automatically according to the current running device resolution to choose which folder to load the picture. Of course, this is just the ideal situation, more time the art will only provide us with a picture, then you put all the pictures in the drawable-hdpi folder is good.
Knowing the meaning of each folder in the Res directory, let's take a look at how to use these resources. Like the Hello world! string we just found in Strings.xml, we have two ways to refer to it:
1. A reference to the string can be obtained by R.string.hello_world in the code;
2. A reference to the string can be obtained through @string/hello_world in the XML. The basic syntax is the above two ways, where the string part is replaceable, if it is a reference to the picture resource
Can be replaced by drawable, if the layout file is referenced can be replaced layout, and so on. Here is no longer a specific example, because you will be in the project to use a large number of resources, then there are many examples. In addition to tell you small, HelloWorld project icon is in the Androidmanifest.xml through the android:icon= "@drawable/ic_launcher" to specify, Ic_launc Her this picture is in the Drawable folder, if you want to modify the item's icon should know what to do?
Android Program Directory Introduction