The meaning and usage summary of the Android program portal and the project folder-Getting Started

Source: Internet
Author: User

    • 1 entrance to Android program1.1 Activity Class

      In the Andriod application, the user perceives a single application interface, in which each application interface corresponds to an activity class, similar to form forms in a. NET WinForm project. Like the form in WinForm, the activity class is also a base class, and each view interface inherits the activity. Let's look at the code of the activity's extension class.

      public class Mainactivity extends Activity {  @Override  void oncreate (Bundle savedinstancestate) {  super. oncreate (savedinstancestate)  setcontentview (R.layout. activity_main); }  @Override  protected void  onstop () { //TODO auto-generated method stub super.< Span class= "Fu" >onstop ();  //Execute Logic Code}}        

      When the activity is created, it executes the OnCreate function, which, of course, executes the corresponding function during the different life cycles of the activity. The OnCreate function is called when it is created, and again, when the activity interface is not visible, the OnStop function is called. This is similar to WinForm, these functions correspond to the formload and formclosing events of the WinForm form, but unlike the FormClosing event, the close of the form can be canceled, and the activity is not, which is well understood, Because in the Android program, you click the Back button yourself, that is sure to go back to the previous interface, the current interface will disappear.

      We can build many activity interface classes in the program, like our WinForm program, can also build multiple form interface, then the problem, so the activity interface, when the program starts, which should start?

      1.2 Androidmanifest.xml configuration file

      In the WinForm program, the startup of the program is generally responsible for starting the first program interface, while in the Android program, the configuration file is used to configure the portal's activity interface. The following is the code in the Androidmanifest.xml configuration file.

      <?xml version= "1.0" encoding= "Utf-8"?><manifestXmlns:android="Http://schemas.android.com/apk/res/android"Package="Com.example.helloworld"Android:versioncode="1"Android:versionname="1.0"><uses-sdkandroid:minsdkversion="16"android:targetsdkversion="21"/><uses-permissionAndroid:name="Android.permission.INTERNET"></uses-permission><applicationandroid:allowbackup="True"android:icon="@drawable/ic_launcher"Android:label= "@string/app_name"  android:theme= "@style/apptheme"  > <activity android:name=  android:label=" @string/app_name " <intent-filter> <action  Android:name= "Android.intent.action.MAIN"  />  android:name= "Android.intent.category.LAUNCHER"  /> </intent-filter> </activity> < span class= "kw" ></application></MANIFEST>       

      This profile is something that every Android project has to have, and the following explains what some nodes mean.

        1. uses-permission : Indicates that the user needs permission to authorize the program, which is configured in the XML above that requires the user to grant access to the network.
        2. Application : This configuration node is important, its child node activity is the configuration of the Android program entrance, Android:name= ". Mainactivity "Configured The program's initial view interface to Mainactivity (this mainactivity class is in the SRC folder). The action and category node configurations are also critical, and the Android.intent.action.MAIN in the action node indicates that the activity is the entry point for the entire application; The Android.intent.category.LAUNCHER in category means that the activity is attributed to the loader class, which is labeled as an activity that automatically loads and starts, This will load the activity when the program starts.

      The current understanding of the Andriod program runs like this. As for the internal how to achieve, at present not deep, do not understand. (Look at the high finger point)

      First, the loader needs a resource, then reads the configuration file, finds the Portal Activity view configured in the application node, and loads the activity view for display.

      < Span class= "OT" > < span class= "kw" >< Span class= "St" >2 the meaning of the project folder              

      After you create a new Android project, the following directory structure appears.

      The main catalogs are SRC, gen, assets, bin, Res.

        1. SRC: The source code that holds the project.
        2. Gen: This file is generated automatically when the project is created, it contains a static class of R.java, which includes a lot of static class (inner Class), each static class static member name corresponds to a resource name in the Res folder, save the index of the resource, it is convenient to obtain resources in the code.
        3. The original file format is stored in the Assets:assets folder and is accessed through Assetmanager in the code.
        4. Bin: Store the compiled apk and resource files.
        5. Res: The resource files required for the project, such as strings, layouts, skins, etc., each of which is stored in the R.java class.
      2.1 src

      The src file mainly contains the Java source code of the project, such as the Activity view class, and other logic processing code.

      2.2 Gen

      The R.java file in the Gen directory holds the resource index in the res file. Indexes are of type int.

      From the top down, the array class holds the resource index of the array, drawable: Save picture Index, ID: Save the control in the layout file Id,layout: Save the index of the layout file, string: The index of the string resource to save.

      Note that the R.java class is a resource index that holds the engineer definition in the current project, while Android. R.java is a pre-defined resource index that holds the android interior.

      2.3 Res

      There are many folders in res, and different files hold different resources.

        1. Drawable: Store picture resources, each add a picture resource, the R.drawble class corresponding to add a picture index. Different drawable folders hold images of different resolutions.
        2. Layout: Layouts of the storage interface, each layout file corresponds to the layout index inside the r.layout, and the index name is the same as the layout file name.
        3. Values: Contains XML resource files, each XML file can hold strings, arrays, styles and other data. The XML file can be created as you want.

      The String.xml file contains two string data, the index of two string data is saved in the R.string class, and the index name matches the value of the name tag in the string resource.

      Adding an array resource, an array resource index is added to the R.array class, and the index name matches the value of the name attribute in the label.

      The ID of each control in a layout file is also added to the R.id class. There are two ways to specify an ID for a control, one is @id/btn, the other is @+id/btn, the first is the ID definition in the R.id class, and the latter is the ID index of a new btn.

      2.4 Assets

      Assets folder mainly save the original file format, such as I need to access the code to load an HTML file, or a txt document, then you need to save the HTML file and TXT document to the assets file.

      < Span class= "OT" > < span class= "kw" >< Span class= "St" >3 use of various resources in the project folder             

      The 2nd section has already figured out what resources are stored in various folders, so this section summarizes how to access these resource files in your program code. (All resource indexes have been automatically updated to the R Class)

      3.1 Using layout files

      The layout file is like a WPF interface layout file, and if no program uses it, it's just an XML file, and we need to render the file through the activity's Setcontentview method before it shows up on the activity view. This design approach enables the separation of UI and business logic code.

      In the activity's OnCreate method, call the layout file and render the interface.

      setContentView(R.layout.activity_main);//实现界面的渲染

      Activity's Setcontentview method, with a parameter, which is the index of the layout file. This method implements interface binding through the layout file index.

      Get controls in a layout file

      TextView myView=(TextView) this.findViewById(R.id.textView1);
      3.2 Getting the various resources under the values folder

      Accessing string resources

      String sstr=this.getResources().getString(R.string.app_name);//获取app名称

      Accessing Array Resources

      int []myarray=this.getResources().getIntArray(R.array.my_array);
      3.3 Getting resources under the assets folder

      To save an HTML page in the assets file, we need to load the HTML page with the WebView control in the code.

      The code is as follows:

      myWebView.loadUrl("file:///android_asset/myblog.html"); //注意获取文件的路径格式 4 总结

      Figuring out the role of the various parts of the project is important to the novice who is just touching Android programming, which is a fundamental thing to understand and to learn more about Android.

    • Above is

The meaning and usage summary of the Android program portal and the project folder-Getting Started

        the content, more

Access to semantic and folder entry and the Android Summary program project

      , please use the search function on the top right to get information.
    • Previous article

The meaning and usage summary of the Android program portal and the project folder-Getting Started

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.