Android Learning Note 4 Android program structure and important Program files

Source: Internet
Author: User

When you create an Android program, ADT automatically creates some directories and files. These directories and files have a fixed effect. Our projects are generally modified and added in these files. Therefore, understanding these files and directories is a very important part of Android development.

These directories and files have their fixed role, some allow modification, and some cannot be modified.

5 more important Folders Src, gen, assets, bin, and Res.

    • The src directory is the source code directory, and all Java files and Java files that are allowed to be modified by the user are saved in this directory.

Here, at the beginning of the Helloandroid project, ADT automatically establishes a Mainactivity.java file based on the "Create Activity" option in the Project Wizard.

    The
    • Gen directory is used to save ADT automatically generated Java files for logging Resources Index , such as R.java or aidl files. The files in this directory do not recommend any changes to the user. (R.java can be understood as a resource is numbered: When a developer adds the appropriate type of file to any subdirectory in the res/directory, ADT automatically generates a constant of type static int in the corresponding inner class in the R.java file, indexing the added file. If a new interface is added to the layout directory, the corresponding static int constant is added to the public static final class layout. Instead, when we delete any file in the Res directory, its corresponding record in R.java will be automatically deleted by ADT, see here for details. The files in this folder need not be changed basically.
    • Assets directory used to store files in the original format
      binary format files such as audio files, video files, and so on.
      Resources In this directory cannot be r.java to the file index , so they can only be read in the form of a byte stream. The default is an empty directory.
      Usually, the background music under the Android app, and so on, will be placed in that folder. The

    • bin directory holds the resulting files during compilation, as well as the resulting apk files. The
      apk file can be installed directly on the Android system.

    • The res directory is a resource directory, and resources such as images, colors, styles, themes, interface layouts, and strings for Android programs are stored in several subdirectories below it. The drawable-hdpi, drawable-mdpi, and drawable-ldpi directories are used to store image files of different sizes that need to be displayed for different screen sizes in the same program. The layout directory is used to store the user interface-related layouts file, and ADT generates the activity_main.xml file in the layout directory. The layout used for mainactivity. The values directory holds resources such as colors, styles, themes, and strings, and ADT generates a Strings.xml file in the values directory that holds the strings used by the application.

Several important documents of this program:

1. androidmanifest.xml file
2. Itemdetailactivity.java Itemlistactivity.java Itemdetailfragment.java Itemlistfragment.java file
3. Activity_item_detail.xml activity_item_list.xml activity_item_twopane.xml fragment_item_detail.xml file
4. strings.xml file
5. R.java file

Androidmanifest.xml file

The file is in the root directory of the project. This file is a configuration file for Android projects. The various components used in the Android project need to be registered in this file. The various permissions required for this project need to be declared in this file, and the minimum and target versions of the program are specified.

The manifest element contains a application element that can declare the four most important components of an Android program, including Activity, Service, Broadcastreceiver and ContentProvider.

The graph shows that there are two activities in this project, namely Itemlistactivity and itemdetailactivity, and only tags registered in this way can be used by Android projects.

Activity Tags <intent-filter> lets you specify how the activity starts.

1 <intent-filter>2         <action android:name= "Android.intent.action.MAIN"/>4         <category android: Name= "Android.intent.category.LAUNCHER"/>5 </intent-filter>

Indicates that Itemlistactivity is the main entry for the project and that the activity will be invoked when the project starts.

Itemdetailactivity.java Itemlistactivity.java Itemdetailfragment.java Itemlistfragment.java file

These files are the Java source code for all the components in this project. Under the SRC folder directory.

Taking Itemlistactivity.java as an example,

protected void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_item_list);}

Indicates that the OnCreate () method is called when activity first starts, invokes the OnCreate () function of the parent class, and passes Savedinstancestate to the parent class, Savedinstancestate is the state information of the activity ;

Setcontentview the user interface that needs to be displayed, which is the interface layout described in the XML language and stored in the Scr/layout/activity_item_list.xml resource file.

Activity_item_detail.xml activity_item_list.xml activity_item_twopane.xml fragment_item_detail.xml file

In Android programming, the logic and view of activity is separate. The Java code for a component uses an XML file as its layout file, using the user interface described in the XML language.

Strings.xml file

res/values/strings.xml files,
Android apps, the interface displays all the strings that should be defined in the file.
<resources>    <string name= "app_name" >KIWI</string>    <string name= "Title_item_detail" >item detail</string></resources>

  Referencing resources

Resource references have two situations: one is to reference a resource in code , and the other is to reference a resource in a resource.
References to resources in your code need to use the ID of the resource, either through [R.resource_type.resource_name] or [Android. R.resource_type.resource_name] Get resource ID
Resource_type represents the resource type, which is the inner class name in the R class
Resource_name represents the resource name, the file name of the corresponding resource, or the resource name attribute defined in the XML file
Resource referenced in resource, reference format: @ [package:]type:name
@ Represents a reference to a resource
Packages are package names, and packages can be omitted if they are in the same package.

Located in the Gen folder.
ADT Auto-generated files
Contains reference pointers to resources within the drawable, layout, and values directories, and the Android program can refer to resources in the catalog directly through the R class.
Therefore, we can assume that R.java is numbering the resources in Res.
It should be noted that the R.java file should not attempt to modify manually, should be automatically generated by the system. Otherwise, there may be an update problem.



 




Android Learning Note 4 Android program structure and important Program files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.