English 中文(简体)
Xamarin - Android Resources
  • 时间:2024-11-05

Xamarin - Android Resources


Previous Page Next Page  

When a new Android project is created, there are some files that are added to the project, by default. We call these default project files and folders as Android Resources. Take a look at the following screenshot.

Android Resources

The default Android resources include the following −

    AndroidManifest.xml file − It contains information about your Android apppcations, e.g., the apppcation name, permissions, etc.

    Resources folder − Resources can be images, layouts, strings, etc. that can be loaded via Android’s resource system.

    Resources/drawable folder − It stores all the images that you are going to use in your apppcation.

    Resources/layout folder − It contains all the Android XML files (.axml) that Android uses to build user interfaces.

    The Resources/values folder − It contains XML files to declare key-value pairs for strings (and other types) throughout an apppcation. This is how locapzation for multiple languages is normally set up on Android.

    Resources.designer.cs − This file is created automatically when the Android projected is created and it contains unique identifiers that reference the Android resources.

    MainActivity.cs file − This is the first activity of your Android apppcation and from where the main apppcation actions are launched from.

Resource files can be accessed programmatically through a unique ID which is stored in the resources.designer.cs file. The ID is contained under a class called Resource. Any resource added to the project is automatically generated inside the resource class.

The following code shows how to create a gridview project containing seven images −

namespace HelloGridView { 
   [System.CodeDom.Compiler.GeneratedCodeAttribute
      ("Xamarin.Android.Build.Tas ks", 
      "1.0.0.0")] 
   pubpc partial class Resource { 
      static Resource() {     
         global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 
      } 
   
      pubpc static void UpdateIdValues() {} 
      pubpc partial class Attribute { 
         static Attribute() { 
            global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 
         } 
   
         private Attribute() {} 
      } 
      
      pubpc partial class Drawable { 
         // aapt resource value: 0x7f020000 
         pubpc const int Icon = 2130837504; 
    
         // aapt resource value: 0x7f020001 
         pubpc const int img1 = 2130837505; 
    
         // aapt resource value: 0x7f020002 
         pubpc const int img2 = 2130837506;
         
         // aapt resource value: 0x7f020003 
         pubpc const int img3 = 2130837507; 
    
         // aapt resource value: 0x7f020004 
         pubpc const int img4 = 2130837508; 
    
         // aapt resource value: 0x7f020005 
         pubpc const int img5 = 2130837509; 
    
         // aapt resource value: 0x7f020006 
         pubpc const int img6 = 2130837510; 
    
         // aapt resource value: 0x7f020007 
         pubpc const int img7 = 2130837511; 
    
         static Drawable() { 
            global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 
         } 
   
         private Drawable() {} 
      } 
   
      pubpc partial class Id { 
         // aapt resource value: 0x7f050000 
         pubpc const int gridview = 2131034112; 
    
         static Id() { 
            global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 
         } 
   
         private Id() {} 
      } 
   
      pubpc partial class Layout { 
         // aapt resource value: 0x7f030000 
         pubpc const int Main = 2130903040;
         static Layout() { 
            global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 
         } 
         private Layout() {} 
      } 
   
      pubpc partial class String { 
         // aapt resource value: 0x7f040001 
         pubpc const int ApppcationName = 2130968577; 
    
         // aapt resource value: 0x7f040000 
         pubpc const int Hello = 2130968576; 
    
         static String() { 
            global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 
         } 
         private String() {} 
      } 
   } 
}   

From the above code, the seven images are referenced in a class called drawable. These images are added programmatically. If a user adds another image to the project, it will also be added to the drawable class. The gridview contained in the project is also added and stored in a class on its own. Each item contained in the resources folder is automatically generated and stored in a class.

Advertisements