Android project structure, how it runs.

Source: Internet
Author: User

To help me understand, I decided to upload a project first. This is a small job I have done in 3 or 4 hours. It is worth nothing, and it is a poor job, here is a template for everyone.

 

I divide a project into six parts, as shown in the picture on the left, and I will explain them one by one.

1. Here we store the classes we have written. Because of my simplicity, I have not divided many packages, so one package can handle everything.

2. this is generated by compilation. There is an R in it. A Java file must not be moved in disorder, which may lead to terrible errors. In addition, if it is clearly correct, but it cannot be compiled, it must be a problem, you can search for the solution on the Internet. I don't know if I can delete this file and re-compile it.

3. these three folders are used to put images, such as buttons made by yourself, and three pixel image folders (mdpi) and ldpi) in high (hdpi, call images of different pixels on different screens.

4. Check the name and you will know about the UI. After the UI is written, it will be called in the java file.

5. This stores some strings. For example, some strings are commonly used. If they are referenced in multiple places, place a name here to share the strings.

6. This is where the activity and permissions are declared as described in the previous article.

 

Therefore, when an android program is running, the program first finds the entry class in the manifest file, and then enters the onCreate () function of the entry class to start execution, the UI is called in the layout folder and the image is called in the res folder.

 

Next, let's go to the code and use the code to analyze it a little, so we can see:

 

First, we can see that the entry class in the manifest file is secretdiaryactivity. Java:

<activity android:name=".SecretDiaryActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Next, go to the secretdiaryactivity. Java file and find the oncreate () function:

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. diary_list );
Mdbhelper = new diarydbadapter (this );
LL = (linearlayout) findviewbyid (R. Id. bg); // Part One
Mdbhelper. open ();
// Code = (edittext)
// Retrieve the log display in all databases
// If (currentbg)

If (mdbhelper. iscode ()){
Intent I = new intent (this, enterpage. Class );
Startactivityforresult (I, activity_enter );
}
Int BG = mdbhelper. getbg ();
Resources resources = getbasecontext (). getresources ();
Drawable btndrawable = resources. getdrawable (R. drawable. bg );
Switch (BG ){
Case 0:
Break;
Case 1:
Btndrawable = resources. getdrawable (R. drawable. bg2 );
Break;
Case 2:
Btndrawable = resources. getdrawable (R. drawable. bg3 );
Break;
}

Ll. setbackgrounddrawable (btndrawable );
Renderlistview (); // Part Two
}
 

You can see that setcontentview (R. layout. diary_list); defines the XML file called by layout of the activity.

By the way, the part one statement is used to declare a UI and locate the Layout Based on the ID. In this case, we need to find a linearlayout named BG, in this case, the second part is the R. in the Java file, the compiler finds the physical address of BG from here. We can see that the definition of BG is actually in the diary_list.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/bg"
>"

<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>"
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
Android: text = "You haven't started writing a diary yet! \ N click the menu button to start writing a diary :)"
Android: layout_margin = "20px"
Android: textsize = "17px"
Android: textstyle = "bold"
Android: textcolor = "#000"
/>
</Linearlayout>

The fifth line of the file defines the ID of a layout. Any control and layout here can give it an ID to facilitate the call and operation in the Java file, after finding it by ID, we can use ll to operate on it. For example, here I use ll to set a background for BG.

This is the general architecture of an Android software. After knowing the operation process, you can easily know when to write something, next, you need to check the official guide for specific functions ~.

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.