Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2108802
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2010-10-11 13:57:01

  1. Load resources from un-installed apk

    点击(此处)折叠或打开

    1.     private void testRes()
    2.     {
    3.         String apkPath = "/sdcard/com.test.plugin.apk";
    4.         String pkgName = "com.example.demoapkloaderlib";
    5.         
    6.         Resources res = loadRes(apkPath);
    7.         if (res != null)
    8.         {
    9.             System.out.println("##################### app_name a = " + res.getString(R.string.app_name));
    10.             System.out.println("##################### app_name b = " + res.getString(res.getIdentifier("app_name", "string", pkgName)));
    11.         }
    12.         else
    13.         {
    14.             System.out.println("##################### failed to load res");
    15.         }
    16.     }
    17.     
    18.     
    19.     private Resources loadRes(String apkPath)
    20.     {
    21.         try
    22.         {
    23.             AssetManager assetMgr = AssetManager.class.newInstance();
    24.             Method mtdAddAssetPath = AssetManager.class
    25.                     .getDeclaredMethod("addAssetPath", String.class);
    26.             mtdAddAssetPath.invoke(assetMgr, apkPath);
    27.             
    28.             Constructor<?> ctorResources = Resources.class
    29.                     .getConstructor(AssetManager.class, DisplayMetrics.class,
    30.                             Configuration.class);
    31.             
    32.             Resources res = (Resources) ctorResources.newInstance(assetMgr,
    33.                     mActivity.getResources().getDisplayMetrics(),
    34.                     mActivity.getResources().getConfiguration());
    35.             
    36.             return res;
    37.         }
    38.         catch (Throwable t)
    39.         {
    40.             t.printStackTrace();
    41.         }
    42.         
    43.         return null;
    44.     }


  2. Reference (Deprecated)
    Resources and Internationalization:  
    Define Language and Screen Orientation dependent resources, Reference Resource in code and resource files. Resource Type:
        * Simple Values (Colors, Strings and Styled Text, Dimensions)
        * Drawables (different screen)
        * Animation
        * Layout
        * Raw resource (Raw data)
        * Styles and Themes

    • http://developer.android.com/guide/topics/resources/resources-i18n.html
    • http://developer.android.com/guide/topics/resources/available-resources.html
    • http://developer.android.com/guide/topics/ui/themes.html
    • http://developer.android.com/reference/android/R.style.html (Available system defined themes, please search "theme" or "theme_" on that page)
    • http://developer.android.com/intl/zh-CN/reference/android/view/Window.html#setBackgroundDrawable(android.graphics.drawable.Drawable) (Window background)
    • http://developer.android.com/reference/android/package-summary.html   (Pre-defined resource id/type)
    • http://developer.android.com/guide/practices/screens_support.html
  3. Docs:
    1. Providing Resources
      http://developer.android.com/guide/topics/resources/providing-resources.html
      Notes: important contents in this webpage.
        - Group Resource Types. Save resource files under res/ directory, such as layout, drawable, values, etc.
        - Qualifier name rules. To specify configuration-specific alternatives, create a new directory in res/ named in the form - to save alternative resources, Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash. (Refer to  'Table 2. Configuration qualifier names.' to get all supported Configuration qualifier names)
        - Creating alias resources. Including bitmap, layout (merged + include), valuess (string, color, dimen, ...)
    2. Creating Reusable UI Components
      http://developer.android.com/resources/articles/layout-tricks-reuse.html
    3. Accessing Resources (in code and XML)
      http://developer.android.com/guide/topics/resources/accessing-resources.html
      - Access resource in code
      - Access resource in XML
      - Access Style attributes (System attributes are defined in android.R.attr)
    4. Process Resources Changes (or System Configuration Changes)
      http://developer.android.com/guide/topics/resources/runtime-changes.html
    5. Resource Types
      http://developer.android.com/guide/topics/resources/available-resources.html
    6. Android Built-in Resource
      Android built-in resources, including style, attr, drawable, layout, string items, are located in /platforms/android-/data/res/
    7. Icon Specifics (Icon Design Guidelines)
      http://developer.android.com/guide/practices/ui_guidelines/icon_design.html

      Include icons for Launcher, menu, status bar, Tab, dialog, list view.
    8. All available attributes
      http://developer.android.com/reference/android/R.styleable.html
    9. xxx
  4. Basic
    To reference to another resource in XML, you must use the form "@[package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

    The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the resource string and identify it as a resource.

    To reference resourde in code, you must use the form "[package].r.type.name"

    Generally, every resource file contains the following lines:

    Define xml version and charset

    xmlns:android=""
    Almost all outer-most elements of resource file contain this line, 'xmlns' shorts for 'XML NameSpace', it declaims the name space to be used, then you can reference the attributes within this name space, ie. if you declaim to use android name space 'xmlns:android="xxx"', the contents between':' and '=' is alias of name space then you can reference 'android:text' later in the resorce file later.


    In resources file, if you want to remove the default value of elements, just set its value to be "@null", ie.
        ....
        android:background="@null"
            />

  5. Resources
    • Docs
      • http://developer.android.com/guide/topics/resources/index.html
    • Summary
      The Android SDK tools compile your application's resources into the application binary at build time. To use a resource, you must install it correctly in the source tree (inside your project's res/ directory) and build your application. As part of the build process, the SDK tools generate symbols for each resource, which you can use in your application code to access the resources.
    • Classes
      • Resources: use this class to access your application's resources.
        1. getAssets
        2. openRawResource
        3. getColor
        4. getColorStateList
        5. getConfiguration
        6. getDimension
        7. getDisplayMetrics
        8. getAnimation
        9. getDrawable
        10. getMovie
        11. getLayout
        12. getIntArray/getStringArray/getTextArray
        13. getBoolean/Integer/String/Text
        14. getIdentifier
          Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". ie.
          int resId1 = getResources().getIdentifier("main_layout", "layout", "com.test.helloworld");
          int resId2 = getResources().getIdentifier("string", "string", "com.test.helloworld");
          String str = getResources().getString(resId2);

        15. xxx
      • xxx
    • xxx
  6. layout
    • Docs
      • Merging Layouts
        http://developer.android.com/resources/articles/layout-tricks-merge.html
      • xxx
    • Compare with Layout
      is a layout alias, compared with an separate layout (FrameLayout, LinearLayout, ...) file, it can reduce the number of levels in view trees.
    • xxx
  7. Theme
  8. Configuration
    • Docs
    • Summary
      Describes all device configuration information that can impact the resources the application retrieves, including both user-specified configuration options (locale and scaling) as well as device configurations (such as input modes, screen size and screen orientation). including:
      • hard keyboard: hide or not
      • keyboard: hide or not, type
      • navigation: hide or not, type
      • orientation: type
      • screen layout: type, size
      • touch screen
      • ui mode
    • Classes & Methods
      • Configuration
        1. diff
          Return a bit mask of the differences between this Configuration object and the given one.
        2. needNewResources
          Determine if a new resource needs to be loaded from the bit set of configuration changes.
        3. xxx
      • Others
        1. Get current configuration
          Configuration config = getResources().getConfiguration();
        2. xxx
      • xxx
    • xxx
  9. Locale & i18n
    • Summary
      Locale represents a language/country/variant combination. Locales are used to alter the presentation of information such as numbers or dates to suit the conventions in the region they describe.
    • Classes
      • Locale
      • xxx
    • xxx
  10. Assets
    • Summary
      • Files in 'assets' under the root dir of project
      • 'assets' can contain sub-dir
    • Classes
      • AssetManager (get through Context.getAssets(), or getResources().getAssets())
        1. getLocales: Get supported locale
        2. list: List all asset files under the specified path relative to 'assets'
        3. open: Open files as
          InputStream, AssetFileDescriptor, XmlResourceParser
        4. xxx
      • xxx
    • xxx
  11. Color
    • Color (Simple value)
      • Docs
        1. http://developer.android.com/guide/topics/resources/more-resources.html#Color
      • Classes
        1. Color
          • Contains pre-defined colors: GREEN, GRAY, xxx
          • Return the alpha/red/green/blue component of a color int.
          • Return a color-int from alpha, red, green, blue components.
          • Parse color: Color.parseColor("#FFCC00");
        2. xxx
      • xxx
    • Color state list
      • Docs
        1. http://developer.android.com/guide/topics/resources/color-list-resource.html
        2. http://developer.android.com/reference/android/content/res/ColorStateList.html
        3. xxx
      • Summary
        Apply a color to View, and will actually change colors depending on the state of View.
      • Classes
        1. ColorStateList
      • Difference with Drawable State List
        Color State List is used for text, such as to setTextColor for TextView, Button;
        Drawable State List is used for background, such as to setBackgroundXXX for Button.
      • xxx
    • xxx
  12. String
    • String item
      • Foramt string
        %1$d %2$s
        %d %s
      • xxx
    • Reload string array from code
      getResources().getStringArray(R.array.xxx);
    • xxx
  13. i18n
    • http://developer.android.com/guide/topics/resources/localization.html
    • http://developer.android.com/resources/tutorials/localization/index.html
  14. Drawable
    • Load drawable
      getResources().getDrawable(R.drawable.xxx);
    • ShapeDrawable
      • Create ShapeDrawable in Java Code
        ShapeDrawable drawable = new ShapeDrawable();
        //Set shape: round rectangle
        drawable.setShape(new RoundRectShape(mMaskDrawableCorners, null, null));
        //Set color
        drawable.getPaint().setColor(PRESSED_BG_COLOR);
        //Set size
        drawable.setBounds(originDrawable.getBounds());


  15. Customize Styleable Resources (Attributes)
    You can find all predefined styleable resources in /platforms/android-x/data/res/values/attrs.xml
    • Add new styleable properties in res/values/attrs.xml, the format of this file is:


             


      eg:



             
             
             

    • Use customzied styleable properties in XML
      Add the following line in .xml
      xmlns:myapp="http://schemas.android.com/apk/res/com.view.customzie"
      Note:
      - you can replace 'myapp' with other name space alias
      - The content after 'apk/res/' is package name of current app.

      then you can use the customized styleable properties in this xml, such as myapp:my_text_size="xxx"
    • Access customzied styleable properties in code
      public MyTextView(Context context, AttributeSet attrs)
          {
              super(context, attrs);
             
              TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);  //'MyTextView' is properties array name defiend in attrs.xml
              mCurSize = ta.getDimension(R.styleable.MyTextView_my_text_size, 20);
              setTextSize(mCurSize);
          }
    • More details, please refer sample in ApiDemo, related files:
      • attrs.xml
      • customize_view_1.xml
      • LabelView.java
      • CustomizeView.java
    • xxx
  16. Access resources of other application
    • From source code
      public abstract Resources getResourcesForApplication (String appPackageName)   (Defined in the class PackageManager)
    • xxx
  17. xmlns
    • If to add the following line in layout xml file, resource manager will search attributes from proper apk automatically
      xmlns:app=""
  18. xxx
阅读(2634) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~