2019-05-31
资源
- 无法直接访问的原生资源,保存在assets目录下
- 可通过R资源清单类访问的资源,保存在res件夹下
常用的访问方式:
findViewById( Id );
Resources res = getResources();
Drawable d=res.getdrawable( Id );
res/raw和assets比较
- (相同点) 两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制
- (不同点)res/raw中的文件会被映射到R.java文件中,访问的时候直接使用资源ID即R.id.filename;assets文件夹下的文件不会被映射到R.java中,访问的时候需要AssetManager类。
InputStream is = getResources().openRawResource(R.id.filename);
AssetManager am = null; am = getAssets(); InputStream is = am.open("filename");
- (不同点)res/raw不可以有目录结构,而assets则可以有目录结构,也就是assets目录下可以再建立文件夹
String资源
- 字符串资源XML文件中的定义(string.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test Resource</string>
<string name="test_str1">从代码中引用</string>
<string name="test_str2">从资源文件中引用</string>
</resources>
- 字符串资源的使用
其他资源文件中直接引用
<TextView
android:text="@string/test_str1"
android:id="@+id/myTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text=""
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
代码中使用
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_string);
myTextView = (TextView)findViewById(R.id.myTextView02);
String str = getString(R.string.test_str2).toString();
myTextView.setText(str);
}
Color资源
颜色值定义
通过红(Red)、绿(G)、蓝(B),以及一个透明度(Alpha)值来表示的,颜色值以#开头,接下来就是Alpha-Red-Green-Blue的形式。其中Alpha可以省略,那么该颜色是完全不透明的。支持常见的四种形式。
#RGB:分别制定红、绿、蓝三原色的值(只支持0~f 这16级颜色)来代表颜色
#ARGB:分别制定红、绿、蓝三原色的值(只支持0~f 这16级颜色)及透明度(只支持0~16这16级透明度)来代表颜色
#RRGGBB分别制定红、绿、蓝三原色的值(支持00~ff 这256级颜色)来代表颜色
#AARRGGBB:分别制定红、绿、蓝三原色的值(只支持00~f f这256级颜色)及透明度(只支持00~ff这256级透明度)来代表颜色
- 颜色资源XML文件中的定义(color.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red_bg">#f00</color>
<color name="blue_text">#0000ff</color>
</resources>
- 颜色资源使用
其他资源文件中引用
<TextView
android:text="测试颜色资源,红色背景,蓝色文字"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/blue_text"
/>
代码中使用
public class TestColorActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_color);
// 引用颜色资源,设置背景色为红色
getWindow().setBackgroundDrawableResource(R.color.red_bg);
}
}
dimen尺寸资源

- Dimen尺寸XML文件中的定义(dimens.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_width">150px</dimen>
<dimen name="text_height">100px</dimen>
<dimen name="btn_width">30mm</dimen>
<dimen name="btn_height">10mm</dimen>
</resources>
- dimen尺寸资源使用
其他资源文件中引用(布局)
<TextView
android:text="@string/test_dimen"
android:id="@+id/myDimenTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="@dimen/text_width"
android:height="@dimen/text_height"
android:background="@color/red_bg"/>
<Button
android:text="@string/test_dimen1"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
代码中使用
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_dimen);
myButton = (Button)findViewById(R.id.Button01);
Resources r = getResources();
float btn_h = r.getDimension(R.dimen.btn_height);
float btn_w = r.getDimension(R.dimen.btn_width);
myButton.setHeight((int)btn_h);
myButton.setWidth((int)btn_w);
}
数组资源
- 数组资源定义(/res/values/arrays.xml)
<array />子元素:定义普通类型数组
< string-array />子元素:定义字符串类型数组
< integer-array />子元素:定义整数数组
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< string-array name="sa">
< item>sa1< /item>
< item>sa2< /item>
< /string-array>
< /resources>
- 数组资源使用(代码中使用)
String[] sa = getResources().getStringArray(R.array.sa);
int[] getIntArray(int id)
TypedArray obtainTypedArray(int id)
[<package_name>.]R.array.*
- 其他 XML使用
@ [<package_name>:] array.*
图片资源
Bitmap File(位图文件):png,jpg,gif格式
Color Drawable(颜色)
Nine-Patch Image(.9.png)
-
图片资源定义
Res/drawable/目录下直接添加位图文件g1.jpg和moto.jpg -
引用
布局资源文件中引用(test_bitmap.xml)
<ImageView
android:id="@+id/bitmapImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/g1"
></ImageView>
代码中使用
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_bitmap);
myImageView = (ImageView)findViewById(R.id.bitmapImageView02);
Resources r = getResources();
Drawable d = r.getDrawable(R.drawable.moto);
myImageView.setImageDrawable(d);
}
StateListDrawable资源
SateListDrawable用于组织多个Drawable对象。当使用SateListDrawable作为目标组件的背景、前景图片时, SateListDrawable对象所显示的Drawable对象会随目标组件状态的改变而启动切换。
SateListDrawable用于动态设置EditText、Button、ImageView等组件在不同状态下的背景/前景显示效果。

- SateListDrawable定义(my_image.xml)
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 指定获得焦点时的颜色 -->
<item android:state_focused="true"
android:color="#f44"
/>
<!-- 指定失去焦点时的颜色 -->
<item android:state_focused="false"
android:color="#111"
/>
</selector>
- SateListDrawable使用(布局文件中引用)
<!-- 使用StateListDrawable资源 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/my_image"
/>
layout布局资源
所谓布局就是组件在Activity中的呈现方式,即组件在界面中位置、间距和对齐方式等。/res/layout/**.xml
标签 | 中文名称 | 说明 |
---|---|---|
LinearLayout | 线性布局 | 分为垂直和水平依次排队放置 |
FrameLayout | 框架布局 | 组件从屏幕的左上角坐标布局组件 |
TableLayout | 表格布局 | 类似表格 |
AbsoluteLayout | 绝对布局 | 需要指定左上角的x,y值 |
RelativeLayout | 相对布局 | 相对左右上下UI布局 |
menu菜单资源
菜单 | 说明 |
---|---|
选项菜单(Options Menu) | 当用户按下menu button按钮时显示的菜单 |
上下文菜单(Context Menu) | 当用户长久按住屏幕,即被注册显示上下文菜单的视图时显示的菜单 |
子菜单(Submenu) | 当用户按下一个菜单的某个选项时弹出的子菜单 |
使用Options Menu:
//1、构造菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/option_normal_1"
android:icon="@mipmap/ic_vpn_key_white_24dp"
android:title="普通菜单1"
app:showAsAction="ifRoom"/>
<item android:id="@+id/option_normal_2"
android:icon="@mipmap/ic_email_white_24dp"
android:title="普通菜单2"
app:showAsAction="always"/>
<item android:id="@+id/option_normal_3"
android:icon="@mipmap/ic_vpn_key_white_24dp"
android:title="普通菜单3"
app:showAsAction="withText|always"/>
<item android:id="@+id/option_normal_4"
android:title="普通菜单4"
app:showAsAction="never"/>
</menu>
//2、重写onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//动态向菜单中添加选项
//meu.add(menu组ID,menu的ID,上级menu的ID,需要显示的文本内容)
//上级menu的ID,只能是int类型,
//Menu.NONE的值为0
//动态创建出来的菜单ID是不会再R.ID中出现的,
//所以,如果需要使用到的话,就需要自行定制规则方便使用
menu.add(Menu.NONE, 123, Menu.NONE, "急急急");
//菜单填充
getMenuInflater().inflate(R.menu.menu_def, menu);
//使菜单显示出来
return true;
}
//3、菜单触发事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_1:
Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_LONG).show();
break;
case R.id.menu_2:
Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_LONG).show();
break;
case R.id.menu_3:
Toast.makeText(MainActivity.this, "我的", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(MainActivity.this, "选择错误", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
任意xml资源
略...
样式和主题资源
避免重复为不同界面中的组件设定属性,方便后期项目维护,充分利用样式和主题。
类似于word的样式管理:一个样式等于一组格式的集合。如果设置某段文本使用某个样式,那么该样式的所有格式将会整体应用于这段文本
1、/res/values文件定义样式
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- 定义一个样式,指定字体大小、字体颜色 -->
<style name="style1">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#00d</item>
</style>
<!-- 定义一个样式,继承前一个颜色 -->
<style name="style2" parent="@style/style1">
<item name="android:background">#ee6</item>
<item name="android:padding">8dp</item>
<!-- 覆盖父样式中指定的属性 -->
<item name="android:textColor">#000</item>
</style>
<style name="CrazyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowFrame">@drawable/window_border</item>
<item name="android:windowBackground">@drawable/star</item>
</style>
</resources>
2、样式使用
<!-- 指定使用style1的样式 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/style1"
style="@style/style1"
/>
<!-- 指定使用style2的样式 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/style2"
style="@style/style2"
/>
使用原始资源
res/raw:使用R.raw.xxx
/assets :
InputStream open(String fileName):根据文件名来获取原始资源对应的输入流
AssetFileDescriptor openFd(String fileName):
国际化和资源自适应
方法:
- 语言INI配置文件 :最常用的一种, 为每种语言单独配置一个文件,修改语言包时不需要重新编译软件。
- Resource 文件 :内容存在于Resource资源文件中, 切换语言时须再次全部获取一次,修改语言包的时候, 需要重新编译软件。
网友评论