Android Res xml

  1. 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
  2. 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 <resources_name>-<config_qualifier> 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 <SDK>/platforms/android-<api-level>/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
  3. 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:
    <?xml version="1.0" encoding="utf-8"?>
    Define xml version and charset

    xmlns:android="http://schemas.android.com/apk/res/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.
    <EditText
        ....
        android:background="@null"
            />

  4. 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
  5. layout
    • Docs
      • Merging Layouts
        http://developer.android.com/resources/articles/layout-tricks-merge.html
      • xxx
    • Compare <merge /> with Layout
      <merge /> is a layout alias, compared with an separate layout (FrameLayout, LinearLayout, ...) file, it can reduce the number of levels in view trees.
    • xxx
  6. Theme
  7. 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
  8. 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
  9. 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
  10. 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
  11. String
    • String item
      • Foramt string
        <string name="xxx">%1$d %2$s</string>
        <string name="xxx" formatted="false">%d %s</string>
      • xxx
    • Reload string array from code
      getResources().getStringArray(R.array.xxx);
    • xxx
  12. i18n
    • http://developer.android.com/guide/topics/resources/localization.html
    • http://developer.android.com/resources/tutorials/localization/index.html
  13. 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());


  14. Customize Styleable Resources (Attributes)
    You can find all predefined styleable resources in <SDK>/platforms/android-x/data/res/values/attrs.xml
    • Add new styleable properties in res/values/attrs.xml, the format of this file is:
      <?xml version="1.0" encoding="utf-8"?>
      <declare-styleable name="<Properties-Array-Name>">
              <attr name="<Property-Name>" format="<Property-Type>" />
      </declare-styleable>

      eg:

      <?xml version="1.0" encoding="utf-8"?>
      <declare-styleable name="MyTextView">
              <attr name="text" format="string" />
              <attr name="textColor" format="color" />
              <attr name="my_text_size" format="dimension" />
      </declare-styleable>
    • 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
  15. Access resources of other application
    • From source code
      public abstract Resources getResourcesForApplication (String appPackageName)   (Defined in the class PackageManager)
    • xxx
  16. xmlns
    • If to add the following line in layout xml file, resource manager will search attributes from proper apk automatically
      xmlns:app="http://schemas.android.com/apk/res-auto"
  17. xxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值