android相关 2009-09-28 15:16:43 资源从原代码中被抽取出来,基于效率考虑,XML文件被编译成二进制、可以快速加载的形式。字符串,同样被压缩为一种更富效率的存储形式。对于android 程序开发,需要它时知道去那里找这些资源就可以了。 (一)资源 android 资源系统了解应用程序中所有非代码资源。使用Resources类访问程序中的资源。一个应用程序的资源在编译时被编译器编译到应用程序中的二进制文件 中,要使用一个资源,你必须把安装源文件树中的正确位置,并编译到你应用程序中,作为编译过程的一部分,每个资源的标记都会被生成,在原代码中可要使用这 些标记-允许编译器验证应用程序中的代码和定义的资源是否匹配。那如何创建和使用资源呢! (二)创建资源 android 支持字符串、位图以及其他很多中类型的资源。每一种资源的语法、格式以及存放的位置,都会根据其类型的不同而不同。一般来自三种文件:XML(除位图和 raw之外的任何文件)、位图文件(图像)以及raw文件(eg:声音.........),事实上,XML文件也有两种不同类型:被原封不动地编译进包 内的文件和被aapt 用来产生资源的文件。这里有一个每种资源类型的列表,包括文件格式、文件描述以及XML文件类型的细节。 在项目中的res/目录的适当的子目录中创建和保存资源文件。Android 有一个资源编译器(aapt),它依照资源所在的子目录及其格式对其进行编译。eg:
全局资源说明(Global Resource Notes) 1、一些资源允许你定义颜色值。android 接受的颜色值可以使用多种web样似的形式-以下集中包含十六进制常数的形式:#RGB,#ARGB,#RRGGBB、#AARRGGBB 2、所有颜色之支持设置透明度(alpha channel value),前两位的十六进制数指定了透明了。0在透明度值是全透明。默认值是不透明。 (三)使用资源 1、代码中使用资源-如何在你代码中对调用资源实例化 R.resource_type.resource_name 或 android.R.resource_type.resource_name 2、从其他资源中引用资源-可以从其他资源中应用资源。重用资源中的公共资源。 @[package:]type/name(@string/hello_world|@android:drawable/opaque_red) Context.getResource()获得与应用程序相关的Resource实例。图像资源的引用和风格的统一, eg: <application android:name="CalendarApplication" android:label="@string/app_label" android:icon="@drawable/app_icon" android:taskAffinity="android.task.calendar"> ............... ................ ................. </application> 主要是通过android:icon="@drawable/app_icon"中的@drawable/app_icon应用资源,这种技巧还可以创建资源之间的引用。eg:可以创建新的drawable 资源作为已存在资源的别名。 <?xml version = "1.0" encoding = "utf-8"?> <resources> <drawable id = "my_background">@android:drawable/theme2_background</drawable> </resources> </xml> 3、支持针对交替配置的交替资源-根据主机硬件的语言或显示配置指定加载不同的资源。 super.on setContentView(R.layout.main); 这看似很简短的两个java 语句,其实要去深刻的理解他的作用,然后在扩展开来,里面的内容,真的很多,很多, 4、应用主题属性,另外一种资源允许引用当前主题中的属性值。这个属性值只能在样式资源和XML属性中使用;他允许你通过将他们改变为当前主题提供的标准化来改变UI元素的外观,而不是提供具体的值。 引用格式如下:?[namespace:]type/name和“@”,也是可选的! 使用系统资源:android.R.type.id 5、支持对替换(Alternate)语言和配置的替换资源 MyApp/ res/ values-en/ . . . . . . Android 支持几种类型的限定服,每一个都有不同的值。把他们连接在资源文件名称的后面,使用短横线(“-”)隔开。可以为每一个文件夹名称添加多个限定符,但是必须按照这里列出的顺序排列, eg: drawable-en-rUS-port-160dpi-finger-qwerty-dpad-480x320/ 比较典型的是,你只需指定的要定义资源的配置选项,可以放弃列表中的任何值,但同时要保证生下的值仍然保持列表中的顺序 MyApp/ res/ drawable-en-rUS-finger/ drawable-port/ drawable-port-160dpi drawable-qwerty 自定义View,eg:API LabelView.java 在res/values/ |_arrays.xml |_attrs.xml |_colors.xml .... 关于该目录 (一),可以被编译成很多中类型的资源XML文件 (二),不象其他的/res/folder,他可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身,XML元素类型控制这些资源应该放在R类的什么地方。 (三),尽管这个文件夹里的文件可以任意命名,不过下面这些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中): arrars.xml 定义数据 colors.xml 定义 color drawable 和颜色的字符串值(color string values).使用Resource.getDrawable()和Resource.getColor()分别获得这些资源。 dimens.xml 定义尺寸值(dimension value),使用Resource.getDimension()获得这些资源值 strings.xml styles.xml 定义样式(style)对象 如下API中定一的一些文件 arrars.xml <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www./licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <resources> <!-- Used in View/Spinner1.java --> <string-array name="colors"> <item>red</item> <item>orange</item> <item>yellow</item> <item>green</item> <item>blue</item> <item>violet</item> </string-array> <!-- Used in View/Spinner1.java --> <string-array name="planets"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> <item>Pluto</item> </string-array> <!-- Used in App/SearchInvoke.java --> <string-array name="search_menuModes"> <item>Search Key</item> <item>Menu Item</item> <item>Type-To-Search</item> <item>Disabled</item> </string-array> <!-- Used in app/dialog examples --> <string-array name="select_dialog_items"> <item>Command on <item>Command two</item> <item>Command three</item> <item>Command four</item> </string-array> <string-array name="select_dialog_items2"> <item>Map</item> <item>Satellite</item> <item>Traffic</item> <item>Street view</item> </string-array> <string-array name="select_dialog_items3"> <item>Every Monday</item> <item>Every Tuesday</item> <item>Every Wednesday</item> <item>Every Thursday</item> <item>Every Friday</item> <item>Every Saturday</item> <item>Every Sunday</item> </string-array> <!-- Used in app/menu examples --> <string-array name="entries_list_preference"> <item>Alpha Option 01</item> <item>Beta Option 02</item> <item>Charlie Option 03</item> </string-array> <!-- Used in app/menu examples --> <string-array name="entryvalues_list_preference"> <item>alpha</item> <item>beta</item> <item>charlie</item> </string-array> </resources> attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="LabelView"> <attr name="text" format="string" /> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> <declare-styleable name="TestView"> <attr name="text1" format="string" /> <attr name="textColor1" format="color" /> <attr name="textSize1" format="dimension" /> </declare-styleable> </resources> colors.xml <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www./licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <resources> <drawable name="red">#7f00</drawable> <drawable name="blue">#770000ff</drawable> <drawable name="green">#7700ff00</drawable> <drawable name="yellow">#77ffff00</drawable> <drawable name="screen_background_black">#ff000000</drawable> <drawable name="translucent_background">#e0000000</drawable> <drawable name="transparent_background">#00000000</drawable> <color name="solid_red">#f00</color> <color name="solid_blue">#0000ff</color> <color name="solid_green">#f0f0</color> <color name="solid_yellow">#ffffff00</color> </resources> LableView.java // Need the following imp // class is in a sub-package. imp imp imp imp imp imp imp /** * Example of how to write a custom subclass of View. LabelView * is used to draw simple text views. Note that it does not handle * styled text or right-to-left writing systems. */ public class LabelView extends View { private Paint mTextPaint; private String mText; private int mAscent; /** * Constructor. This version is on * the object manually (not from a layout XML file). * @param context */ public LabelView(Context context) { super(context); initLabelView(); } /** * Construct object, initializing with any attributes we understand from a * layout file. These attributes are defined in * SDK/assets/res/any/classes.xml. * * @see android.view.View#View(android.content.Context, android.util.AttributeSet) */ public LabelView(Context context, AttributeSet attrs) { super(context, attrs); initLabelView(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequence s = a.getString(R.styleable.LabelView_text); if (s != null) { setText(s.toString()); } // Retrieve the color(s) to be used for this view and apply them. // Note, if you on // can instead call a.getColor() and pass that to setTextColor(). setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000)); int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0); if (textSize > 0) { setTextSize(textSize); } a.recycle(); } private final void initLabelView() { mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(16); mTextPaint.setColor(0xFF000000); setPadding(3, 3, 3, 3); } /** * Sets the text to display in this label * @param text The text to display. This will be drawn as on */ public void setText(String text) { mText = text; requestLayout(); invalidate(); } /** * Sets the text size for this label * @param size Font size */ public void setTextSize(int size) { mTextPaint.setTextSize(size); requestLayout(); invalidate(); } /** * Sets the text color for this label. * @param color ARGB value for the text */ public void setTextColor(int color) { mTextPaint.setColor(color); invalidate(); } /** * @see android.view.View#measure(int, int) */ @Override protected void on setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); } /** * Determines the width of this view * @param measureSpec A measureSpec packed into an int * @return The width of the view, honoring constraints from measureSpec */ private int measureWidth(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { // We were told how big to be result = specSize; } else { // Measure the text result = (int) mTextPaint.measureText(mText) + getPaddingLeft() + getPaddingRight(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; } /** * Determines the height of this view * @param measureSpec A measureSpec packed into an int * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); mAscent = (int) mTextPaint.ascent(); if (specMode == MeasureSpec.EXACTLY) { // We were told how big to be result = specSize; } else { // Measure the text (beware: ascent is a negative number) result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop() + getPaddingBottom(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; } /** * Render the text * * @see android.view.View#on */ @Override protected void on super.on canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint); } } custom_view_1.xml <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www./licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Demonstrates defining custom views in a layout file. --> <LinearLayout xmlns:android="http://schemas./apk/res/android" xmlns:app="http://schemas./apk/res/custom_view_dir" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <custom_view_dir android:background="@drawable/red" android:layout_width="fill_parent" android:layout_height="wrap_content" app:text="Red"/> <custom_view_dir android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" app:text="Blue" app:textSize="20dp"/> <custom_view_dir android:background="@drawable/green" android:layout_width="fill_parent" android:layout_height="wrap_content" app:text="Green" app:textColor="#ffffffff" /> </LinearLayout> 转自 http://hi.baidu.com/sun_star_mao/blog/item/ca4c86a84377c9f51e17a202.html |
|
来自: techres > 《Android界面》