【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 
            
|   目录Directory  |  
       资源类型Resource Types  |  
    
|   res/anim/  |  
       XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象  |  
    
|   res/drawable/  |  
       .png、.9.png、.jpg文件,它们被编译进以下的Drawable资源子类型中: 要获得这种类型的一个资源,可以使用Resource.getDrawable(id) 为了获取资源类型,使用mContext.getResources().getDrawable(R.drawable.imageId) 注意:放在这里的图像资源可能会被aapt工 具自动地进行无损压缩优化。比如,一个真彩色但并不需要256色的PNG可能会被转换为一个带调色板的8位PNG。这使得同等质量的图片占用更少的资源。 所以我们得意识到这些放在该目录下的二进制图像在生成时可能会发生变化。如果你想读取一个图像位流并转换成一个位图(bitmap),请把图像文件放在 res/raw/目录下,这样可以避免被自动优化。  |  
    
|   res/layout/  |  
       被编译为屏幕布局(或屏幕的一部分)的XML文件。参见布局声明(Declaring Layout)  |  
    
|   res/values/  |  
       可以被编译成很多种类型的资源的XML文件。 注意: 不像其他的res/文件夹,它可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身。XML元素类型控制这些资源应该放在R类的什么地方。 尽管这个文件夹里的文件可以任意命名,不过下面使一些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中): array.xml 定义数组 colors.xml 定义color drawable和颜色的字符串值(color string values)。使用Resource.getDrawable()和Resources.getColor()分别获得这些资源。 dimens.xml定义尺寸值(dimension value)。使用Resources.getDimension()获得这些资源。 strings.xml定义字符串(string)值。使用Resources.getString()或者Resources.getText()获取这些资源。getText()会保留在UI字符串上应用的丰富的文本样式。 styles.xml 定义样式(style)对象。  |  
    
|   res/xml/  |  
       任意的XML文件,在运行时可以通过调用Resources.getXML()读取。  |  
    
|   res/raw/  |  
       直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。  |  
    
2.自动生成的R class
3. 在代码中使用资源
- // Load a background for the current screen from a drawable resource.
 - this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
 - // WRONG Sending a string resource reference into a
 - // method that expects a string.
 - this.getWindow().setTitle(R.string.main_title);
 - // RIGHT Need to get the title from the Resources wrapper.
 - this.getWindow().setTitle(Resources.getText(R.string.main_title));
 - // Load a custom layout for the current screen.
 - setContentView(R.layout.main_screen);
 - // Set a slide in animation for a ViewFlipper object.
 - mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
 - R.anim.hyperspace_in));
 - // Set the text on a TextView object.
 - TextView msgTextView = (TextView)findViewByID(R.id.msg);
 - msgTextView.setText(R.string.hello_message);
 
- //在屏幕上显示标准应用程序的图标
 - public class MyActivity extends Activity {
 - public void onStart() {
 - requestScreenFeatures(FEATURE_BADGE_IMAGE);
 - super.onStart();
 - setBadgeResource(android.R.drawable.sym_def_app_icon);
 - }
 - }
 - //应用系统定义的标准"绿色背景"视觉处理
 - public class MyActivity extends Activity
 - public void onStart() {
 - super.onStart();
 - setTheme(android.R.style.Theme_Black);
 - }
 - }
 
4. xml文件内引用资源
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <string name="hello">Hello World, HelloDemo!</string>
 - </resources>
 
5. 替换资源(为了可替换的资源和配置)
6. Color Value
- <color name="color_name">#color_value</color>
 
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <color name="opaque_red">#f00</color>
 - <color name="translucent_red">#80ff0000</color>
 - </resources>
 
7.Color Drawables
- <drawable name="color_name">color_value</drawable>
 
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <drawable name="opaque_red">#f00</drawable>
 - <drawable name="translucent_red">#80ff0000</drawable>
 - </resources>
 
8. 图片
9. dimension
- <dimen name="dimen_name">dimen_value单位</dimen>
 
Java: float dimen = Resources.getDimen(R.dimen.some_name)
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <dimen name="one_pixel">1px</dimen>
 - <dimen name="double_density">2dp</dimen>
 - <dimen name="sixteen_sp">16sp</dimen>
 - </resources>
 
10. string
- //不使用转义符则需要用双引号包住整个string
 - <string name="good_example">"This'll work"</string>
 - //使用转义符
 - <string name="good_example_2">This'll also work</string>
 - //错误
 - <string name="bad_example">This won't work!</string>
 - //错误 不可使用html转义字符
 - <string name="bad_example_2">XML encodings won't work either!</string>
 
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <string name="simple_welcome_message">Welcome!</string>
 - <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
 - </resources>
 
- <TextView
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:textAlign="center"
 - android:text="@string/simple_welcome_message"/>
 
- // Assign a styled string resource to a TextView on the current screen.
 - CharSequence str = getString(R.string.styled_welcome_message);
 - TextView tv = (TextView)findViewByID(R.id.text);
 - tv.setText(str);
 
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <string name="search_results_resultsTextFormat">%1$d results for <b>&quot;%2$s&quot;</b></string>
 - </resources>
 
- //title是我们想赋值给%2$s的字符串
 - String escapedTitle = TextUtil.htmlEncode(title);
 
- String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
 - String resultsText = String.format(resultsTextFormat, count, escapedTitle);
 - CharSequence styledResults = Html.fromHtml(resultsText);
 
11. assets文件夹资源的访问
以下为从Raw文件中读取:
代码
try {
InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
以下为直接从assets读取
代码
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
当然如果你要得到内存流的话也可以直接返回内存流!
来源:oschina
链接:https://my.oschina.net/u/117384/blog/23614