Android Android-android Application Structure Analysis (4), android Application Development

Source: Internet
Author: User

Android Android-android Application Structure Analysis (4), android Application Development

Description of the automatically generated R. java File


 1 public final class R { 2     public static final class attr { 3     } 4     public static final class dimen { 5         /**  Default screen margins, per the Android Design guidelines.  6  7          Customize dimensions originally defined in res/values/dimens.xml (such as 8          screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. 9     10          */11         public static final int activity_horizontal_margin=0x7f040000;12         public static final int activity_vertical_margin=0x7f040001;13     }14     public static final class drawable {15         public static final int ic_launcher=0x7f020000;16     }17     public static final class id {18         public static final int action_settings=0x7f080002;19         public static final int btn_change=0x7f080001;20         public static final int tv_info=0x7f080000;21     }22     public static final class layout {23         public static final int activity_main=0x7f030000;24         public static final int second_layout=0x7f030001;25     }


The annotations in the R class show that R. java is automatically generated by the aapt tool based on the resource files in the application. Therefore, we can regard R. java as a resource dictionary file of the Android application.

The rules for apt to generate the R. java file are as follows:

  • Each type of resource corresponds to an internal class of the R class.
  • Each specific resource corresponds to an attribute of the internal class.

Therefore, remember that the names in res must be standardized because they will generate the corresponding attribute names in the R file and comply with the java Naming rules.

Res directory description


Res is short for resource. This directory stores all resources used by Android apps, including images, la S, string resources, color resources, and size resources.

Android puts different resources in different folders according to the conventions, so that the aapt tool can scan these resources and generate the corresponding resource list for them. R. java

Open the res/values/strings. xml file and you can see that the content of this file is very simple.


1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3 4     <string name="app_name">FirstAndroid</string>5     <string name="action_settings">Settings</string>6     <string name="hello_world">Hello world!</string>7 8 </resources>

This resource file defines three string constants, app_name is a constant name, And FirstAndroid is a constant value. Once this resource file is defined, the Android project allows you to use the string resources defined in this resource file in java code and XML Code respectively.

♦Use resources in Java code

In order to use resources in Java code, aapt will generate an R. in the java file, the R class defines an internal class for each resource. Each resource corresponds to an int type attribute in the internal class. For example, in the strings. xml resource file, the content generated in the R file is as follows:


1  public static final class string {2         public static final int action_settings=0x7f050001;3         public static final int app_name=0x7f050000;4         public static final int hello_world=0x7f050002;5     }

With the R class, we can use R. string. app_name to reference the "FirstAndroid" string constant in Java code.

♦Use resources in XML Code

It is easier to reference resources in XML. You only need to access the resources in the following format:

@ <Class Name of the internal class corresponding to the resource>/<name of the resource>

For example, I want the layout text of activity_main.xml to display our app_name

Modify the TextView attribute in activity_main.xml.

Assets Directory description

Native Resource folder. The files here will not be generated by aapt to the R file.

Libs directory description

Library folder, which generally stores jar and so files

AndroidManifest. xml file description

AndroidManifest. the xml configuration file is required by every Android application. It is the global description file of the entire application, AndroidManifest. the xml configuration file describes the name, icon, and components of the application.

The Androidmanifest file usually contains the following information:

The package name of the application. The package name is the unique identifier of the application.

Components contained in applications (such as activity and service)

Minimum application compatibility version

Permission statement required by the application to use the system

The following is a simple Androidmanifest configuration file.


 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="" 3     package="" 4     android:versionCode="1" 5     android:versionName="1.0" > 6  7     <uses-sdk 8         android:minSdkVersion="8" 9         android:targetSdkVersion="17" />10 11     <application12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             android:name=""18             android:label="@string/app_name" >19             <intent-filter>20                 <action android:name="android.intent.action.MAIN" />21 22                 <category android:name="android.intent.category.LAUNCHER" />23             </intent-filter>24         </activity>25         </application>26 27 </manifest>

Permission description

An Android application may need permissions to call functions of the Android system.

Declare the permissions required to run the application

For <manifest... /> Add <uses-permission... /> The sub-element can declare permissions for this application.

For example, if your program needs to access the network, the network permission must be declared.

<! -- Access Network permissions -->
<Uses-permission android: name = "android. permission. INTERNET"/>

In fact, permission usage is not difficult. You can find all permissions required for android on the official website. We will not list them here.



How does one implement the four buttons below for Android Application Development?

Decompiled the layout of 5.0 main_tab.xml.
<? Xml version = "1.0" encoding = "UTF-8"?> <TabHost android: id = "@ id/tabhost" android: layout_width = "fill_parent" android: layout_height = "fill_parent" xmlns: android = "<FrameLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <LinearLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <FrameLayout android: id = "@ android: id/tabcontent" android: layout_width = "fill_parent" android: layout_height = "0.0dip" android: layout_weight = "1.0"/> <TabWidget android: id = "@ android: id/tabs" android: visibility = "gone" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "0.0"/> <LinearLayout android: gravity = "bottom" android: layout_gravity = "bottom" android: orientation = "horizontal" android: id = "@ id/main_tab_group" android: background = "@ drawable/mmfooter_bg" android: paddingTop = "2.0dip" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <FrameLayout android: background = "@ null" android: layout_width = "0.0dip" android: layout_height = &...... remaining full text>

Android app development decryption Chapter 4-5 example crashes. Let me know, the code is as follows, log view

If you want to access the contact, add:
# <! -- Read contact permissions -->
# <Uses-permission android: name = "android. permission. READ_CONTACTS"/>

If you want to call, add
# <! -- Call permission -->
# <Uses-permission android: name = "android. permission. CALL_PHONE"/>

Obviously, you need to read the contact information. AndroidManifest. xml uses-permission

Related Article

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: 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.