- 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
- Docs:
- 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, ...)
- Creating Reusable UI Components
http://developer.android.com/resources/articles/layout-tricks-reuse.html - 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)
- Process Resources Changes (or System Configuration Changes)
http://developer.android.com/guide/topics/resources/runtime-changes.html
- Resource Types
http://developer.android.com/guide/topics/resources/available-resources.html
- 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/
- 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.
- All available attributes
http://developer.android.com/reference/android/R.styleable.html
- xxx
- Providing Resources
- 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"
/> - 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.
- getAssets
- openRawResource
- getColor
- getColorStateList
- getConfiguration
- getDimension
- getDisplayMetrics
- getAnimation
- getDrawable
- getMovie
- getLayout
- getIntArray/getStringArray/getTextArray
- getBoolean/Integer/String/Text
- 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);
- xxx
- xxx
- Resources: use this class to access your application's resources.
- xxx
- Docs
- layout
- Docs
- Merging Layouts
http://developer.android.com/resources/articles/layout-tricks-merge.html - xxx
- Merging Layouts
- 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
- Docs
- Theme
- 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
- hard keyboard: hide or not
- Classes & Methods
- Configuration
- diff
Return a bit mask of the differences between this Configuration object and the given one.
- needNewResources
Determine if a new resource needs to be loaded from the bit set of configuration changes.
- xxx
- diff
- Others
- Get current configuration
Configuration config = getResources().getConfiguration(); - xxx
- Get current configuration
- xxx
- Configuration
- xxx
- 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
- Summary
- 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())
- getLocales: Get supported locale
- list: List all asset files under the specified path relative to 'assets'
- open: Open files as
InputStream, AssetFileDescriptor, XmlResourceParser
- xxx
- getLocales: Get supported locale
- xxx
- AssetManager (get through Context.getAssets(), or getResources().getAssets())
- xxx
- Summary
- Color
- Color (Simple value)
- Docs
- http://developer.android.com/guide/topics/resources/more-resources.html#Color
- Classes
- 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");
- xxx
- Color
- xxx
- Docs
- Color state list
- Docs
- http://developer.android.com/guide/topics/resources/color-list-resource.html
- http://developer.android.com/reference/android/content/res/ColorStateList.html
- xxx
- Summary
Apply a color to View, and will actually change colors depending on the state of View.
- Classes
- 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
- Docs
- xxx
- Color (Simple value)
- String
- String item
- Foramt string
<string name="xxx">%1$d %2$s</string>
<string name="xxx" formatted="false">%d %s</string>
- xxx
- Foramt string
- Reload string array from code
getResources().getStringArray(R.array.xxx); - xxx
- String item
- i18n
- http://developer.android.com/guide/topics/resources/localization.html
- http://developer.android.com/resources/tutorials/localization/index.html
- http://developer.android.com/guide/topics/resources/localization.html
- 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());
-
- Create ShapeDrawable in Java Code
-
- Load drawable
- 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
- Add new styleable properties in res/values/attrs.xml, the format of this file is:
- Access resources of other application
- From source code
public abstract Resources getResourcesForApplication (String appPackageName) (Defined in the class PackageManager)
- xxx
- From source code
- 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"
- If to add the following line in layout xml file, resource manager will search attributes from proper apk automatically
- xxx
Android Res xml
最新推荐文章于 2021-05-28 09:57:12 发布