现在的位置: 首页 > 综合 > 正文

AndroidPro4_001_Resources

2013年09月10日 ⁄ 综合 ⁄ 共 4120字 ⁄ 字号 评论关闭
# Resource-Reference Syntax
\ Structure:@[package:]type/name.
\ Type is one of the resource-type in R.java: drawable, layout, id, String, attr, plurals, string-array.
\ If don't specify package, the pair type/name base on local resource, and the application's local R.java package.
\ <TextView android:id="@+id/text1"/>: The "+" indicates if the id of "text1"is not define as a resource, then define it.
# Define IDs for later use(Predefine an ID)
\ To predefine an ID, use a resource tag: item
<resources>
<item type="id" name="text1"/>
</resources>
type指resources type, 本例为id;
# The /res folder :
/res/anim (Compiled animation files)
/res/drawable (Bitmaps)
/res/layout (UI and view definitions)
/res/values (Arrays, colors, dimensions, strings, and styles)
/res/xml (Compiled arbitrary xml files)
/res/raw (Noncompiled raw files)
# Image Resources
\ Image resources 不能重名,会出错,另外,/res/drawable 下如果有子目录,内部文件会被忽略.
\ 在XML中drawable资源引用 “@drawable/sample_image"
\ java code中drawable资源引用 :
//call getDrawable() to get the image
BitmapDrawable d = activity.getResources().getDrawable(R.drawable.sample_image);
\ Use the drawable to set the background :
//set the background directly from resource ID
button.setBackgrondResources(R.drawable.sample_image);
\ The background methods go all the way back to the View class. As a result, most of the UI controls have this background support.
\ stretchable image 可拉伸图像,使用.png格式,use Draw 9-path tool to specify these regions.
# Color-Drawable Resources
\ Android support image resources and color-drawable resources, color-drawable resources is essentially a colored rectangle.
\ Xml syntax for defining color-drawable resources
//under /res/value folder
<resources>
<drawable name="red_rectangle">#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
</resources>
\ Use color-drawable in Java code :
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlign="center"
android:background="@drawable/red_rectangle"/>
\ 如果要使用圆角(rounded corners),使用<shape> tag.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f0600000"/>
<stroke android:width="3dp" color="#ffff8050"/>
<corners android:radius="13dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp"/>
</shape>
\ 这种/res/drawable下的xml格式在JAVA CODE中这样用:
//Get the drawable
GradientDrawable roundedRectangle = (GradientDrawable) activity.getResources().getDrawable(R.drawable.ooxx);
//Set it as a background to a text view
textView.setBackgroundDrawable(roundedRectangle);
\ 总的来说:
bitmap images (PNG,BMP之类格式) ->/res/drawable目录下 ->BitmapDrawable
drawable resources(XML格式) ->/res/value目录下 ->ColorDrawable
XML file with <shape> tag ->/res/drawable目录下 ->GradientDrawable
# Arbitrary XML Resource Files (任意的XML文件,在 /res/xml 下)
\ Get instance of XmlPullParser :
//can use it in any context (including activity)
Resources r = activity.getResources();
XmlResourceParser xpp = r.getXml(R.xml.test);
//getXml() return an instance of XmlPullParser
# Raw Resources
\ Raw file such as audio, video... in /res/raw floder
\ Reading Raw Resources (txt)
Resources r = activity.getResources();
InputStream is = r.openRawResource(R.raw.test);
String myText = convertStreamToString(is);//这是一个用户自定义函数
is.close();
//下面是函数定义
String convertStreamToString(InputStream is) throw IOException 
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = is.read();
while( i != -1) {
baos.write(i);
i = is.read();
}
return baos.toString();
}
# Assets
\ Files in "/assets" do not generate IDs in R.java. 
Must specify the file path to read them.
The file path is a relative path starting at /assets.
Use the AssetManager class to access these files.
\ Reading Assets
String getStringFromAssetFile(Activity activity)
{
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
String s = converStreamToString(is);
is.close();
return s;
}
# The Resources Directory Structure
/res/value/string.xml
/colors.xml
/dimens.xml
/attrs.xml
/styles.xml
/drawable/*.png
/*.jpg
/*.gif
/*.9.png
/anim/*.xml
/layout/*.xml
/raw/*.*
/xml/*.xml
/assets/*.*/*.*
# Alternate Resource Directories
/res/layout/main_layout.xml
/res/layout-port/main_layout.xml
/ren/layout-land/main_layout.xml
These 3 layout files generate only 1 layout ID in R.java(R.layout.main_layout)
\ Directory extensions -port and -land called configuration qualifiers.(directory name with a hyphen "-" )
\ Resources specify in these configuration qualifiers directory are called Alternate  Resources
\ Resources in /res without configuration qualifiers are called Default Resources

抱歉!评论已关闭.