一.android中的资源是在代码中使用的外部文件。图片,音频,动画和字符串等叫做android中的资源文件。
二.Android工程 资源类型布局表
与src源文件夹并列的两个文件夹assets和res用来保存资源文件。
1.assets文件夹中放原声文件如MP3文件,通过AssetManager类以二进制流的形式访问
2.res中资源可以通过R资源类直接访问:
anim:保存动画
drawable:位图文件
layout:xml布局文件
values:各种xml资源文件
arrays.xml:xml数组文件
colors.xml:xml颜色文件
dimens.xml:xml尺寸文件
styles.xml:xml样式文件
raw:直接复制到设备中的源文件
menu:xml菜单文件
使用mContext.getResources()得到Resources对象来获取资源
XML的写法如下:
- 代码
-
- 一个android工程中,有各种类型的资源文件,大致可以分为以下几种:
- 、 颜色 #RGB #ARGB #RRGGBB #AARRGGBB
-
-
颜色资源应该位于<resourses></resourses>标签下
-
- 路径res/values/colors.xml 名字可以随意
-
-
定义<color name=”cname”>value</color>
-
- 使用 Resourse.getValues.getColor
-
-
<?xml version="1.0" encoding="utf-8"?>
-
-
<resources>
-
-
<color name="white">#FFFFFF</color>
-
-
<color name="black">#000000</color>
-
-
</resources>
- 、 字串
-
-
字串资源应该位于<resourses></resourses>标签下
-
- 路径res/values/strings.xml
-
-
定义<String name=”sname”>value</String>
-
- 使用 Resourse.getValues.getString
-
-
<?xml version="1.0" encoding="utf-8"?>
-
-
<resources>
-
-
<string name="hello">Hello World, ResrouseTestActivity!</string>
-
-
<string name="app_name">ResrouseTest</string>
-
-
</resources>
- 、 图片
-
- 图片资源一般使用png格式,使用其他格式的会出现各种问题,貌似不支持gif格式的图片,可是使用Movie来播放gif格式的图片
-
- 路径res/drawable
-
- 可以直接存放图片也可以是xml等配置文件(一般用于自定义组件)
-
- 使用 getDrawable
- 、 图片的颜色
-
- 位于res/values/my_drawable.xml名字随意
-
-
<?xml version="1.0" encoding="utf-8"?>
-
-
<resources>
-
-
<drawable name="solid_red">#FF0000</drawable>
-
-
</resources>
-
- 定义用于填充一个组件的颜色值,即给view设置背景色。用法和drawable下的图片一样,其实没多少意义,使用颜色定义就ok了,目前我是这样认为的,可能有更好的优点,不过我没发现罢了,嘿嘿,continue...
- 、 单位资源
-
-
单位资源应该位于<resourses></resourses>标签下
-
- 路径res/values/dimen.xml 名字可以随意
-
- 使用和String、color类似
-
-
<?xml version="1.0" encoding="utf-8"?>
-
-
<resources>
-
-
<dimen name="dimen_name">2px</dimen>
-
-
<dimen name="dimen_px">5px</dimen>
-
-
<dimen name="dimen_pt">3pt</dimen>
-
-
<dimen name="dimen_dp">3dp</dimen>
-
-
</resources>
- 、 Nine-patch(可以拉伸的小图片)
-
- 支持图片的拉伸
-
- 貌似就是所谓的png图片文件资源,图片在应用view的背景时,如果被设为background则会随view的大小变化做相应的拉伸和收缩,像ImageView这类设置src图片则不随view变化,按其自身大小显示部分或全部!
- 、 菜单
-
-
菜单即可以从代码中实现也可以在资源文件中配置,这里就是要描述一下第二种<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/previous" android:title="@string/previous" android:enabled="false" android:icon="@android:drawable/ic_media_previous"/> <item android:id="@+id/play_pause" android:title="@string/play" android:icon="@android:drawable/ic_media_play"/> <item android:id="@+id/next" android:title="@string/next" android:icon="@android:drawable/ic_menu_next"/></menu>8、 Layout布局
-
- 这个就是你经常看到的与用户交互的界面的xml文件,就是各个view的排列和嵌套,没什么好说的啦
- 、 风格和主题、
-
- 风格主要是指view的显示风格 res/values/filename.xml
-
-
<?xml version=”1.0″ encoding=”utf-8″?>
-
<resources>
-
<style name=”SpecialText” parent=”@style/Text”>
-
<item name=”android:textSize”>18sp</item>
-
<item name=”android:textColor”>#008</item>
-
</style>
-
</resources>
-
-
主题主要针对Activity等, 可以在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里.风格可以自己定义也可以使用程序自带的或是继承已有的风格。
-
-
<?xml version="1.0" encoding="utf-8"?><resources><style name="CustomTheme"><item name="android:windowNoTitle">true</item><item name="windowFrame">@drawable/screen_frame</item><item name="windowBackground">@drawable/screen_background_white</item><item name="panelForegroundColor">#FF000000</item><item name="panelBackgroundColor">#FFFFFFFF</item><item name="panelTextColor">?panelForegroundColor</item><item name="panelTextSize">14</item><item name="menuItemTextColor">?panelTextColor</item><item name="menuItemTextSize">?panelTextSize</item></style></resources>
- 、 动画
-
- 动画资源分为两种,一是实现图片的translate、scale、rotate、alpha四种变化。还可以设置动画的播放特性;另一种是帧动画,逐帧播放设置的资源
-
- 先说一下第一种
-
-
Res/anim/filename.xml<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/accelerate_interpolator" android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0" android:toYDelta="180" android:duration="2000" /> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0" android:toYScale="2.0" android:pivotX="150%" android:pivotY="150%" android:duration="2000" /> <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="@android:integer/config_mediumAnimTime" /> <rotate ....各个属性></rotate> <Interpolator >可以使用其子类和属性定义动画的运行方式,先快后慢,先慢后快等</Interpolator></set> 具体参数的用法,大家可以自己查资料
-
- 下面是第二种资源
-
-
<animation-list xmlns:android=”http://schemas.android.com/apk/res/android”
-
android:oneshot=”true”>
-
<item android:drawable=”@drawable/rocket_thrust1″ android:duration=”200″ />
-
<item android:drawable=”@drawable/rocket_thrust2″ android:duration=”200″ />
-
<item android:drawable=”@drawable/rocket_thrust3″ android:duration=”200″ />
-
</animation-list>
-
-
-
-
-
-
<script src="/javascripts/tinymce/plugins/javaeye/langs/zh.js" type="text/javascript"></script> rif;">1. 相关文件夹介绍 在Android项目文件夹里面,主要的资源文件是放在res文件夹里面的。assets文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像xml,java文件被预编译,可以存放一些图片,html,js, css等文件。在后面会介绍如何读取assets文件夹的资源!
代码示例
/Chapter03_Resource/src/com/amaker/test/MainActivity.java
- 代码
-
-
package com.amaker.test;
-
-
import android.app.ListActivity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.ArrayAdapter;
-
import android.widget.ListView;
-
-
import com.amaker.ch03.color.TestColorActivity;
-
import com.amaker.ch03.dimen.TestDimensionActivity;
-
import com.amaker.ch03.drawable.TestBitmapActivity;
-
import com.amaker.ch03.layout.TestLayoutActivity;
-
import com.amaker.ch03.menu.TestMenuActivity;
-
import com.amaker.ch03.string.TestStringActivity;
-
import com.amaker.ch03.xml.TestXmlActivity;
-
-
public class MainActivity extends ListActivity {
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
-
-
String[] items = {"Test Color","Test String","Test Dimension","Test XML","Test Bitmap","Test Menu","Test Layout"};
-
-
setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, items));
-
getListView().setTextFilterEnabled(true);
-
- }
-
-
-
protected void onListItemClick(ListView l, View v, int position, long id) {
-
Intent intent = null;
-
switch (position) {
-
case 0:
-
intent = new Intent(MainActivity.this,TestColorActivity.class);
- startActivity(intent);
-
break;
-
case 1:
-
intent = new Intent(MainActivity.this,TestStringActivity.class);
- startActivity(intent);
-
break;
-
case 2:
-
intent = new Intent(MainActivity.this,TestDimensionActivity.class);
- startActivity(intent);
-
break;
-
case 3:
-
intent = new Intent(MainActivity.this,TestXmlActivity.class);
- startActivity(intent);
-
break;
-
case 4:
-
intent = new Intent(MainActivity.this,TestBitmapActivity.class);
- startActivity(intent);
-
break;
-
case 5:
-
intent = new Intent(MainActivity.this,TestMenuActivity.class);
- startActivity(intent);
-
break;
-
case 6:
-
intent = new Intent(MainActivity.this,TestLayoutActivity.class);
- startActivity(intent);
-
break;
- }
- }
- }
获取颜色资源
/Chapter03_Resource/src/com/amaker/ch03/color/TestColorActivity.java
- 代码
-
-
package com.amaker.ch03.color;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import com.amaker.test.R;
-
-
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);
- }
- }
/Chapter03_Resource/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red_bg">#f00</color>
<color name="blue_text">#0000ff</color>
</resources>
尺寸资源资源
/Chapter03_Resource/src/com/amaker/ch03/dimen/TestDimensionActivity.java
- 代码
-
- package com.amaker.ch03.dimen;
-
- import android.app.Activity;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.widget.Button;
-
- import com.amaker.test.R;
-
- public class TestDimensionActivity extends Activity {
- private Button myButton;
- @Override
- 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);
- }
- }
/Chapter03_Resource/res/values/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>
Bitmap资源
/Chapter03_Resource/src/com/amaker/ch03/drawable/TestBitmapActivity.java
- 代码
-
-
package com.amaker.ch03.drawable;
-
-
import android.app.Activity;
-
import android.content.res.Resources;
-
import android.graphics.drawable.Drawable;
-
import android.os.Bundle;
-
import android.widget.ImageView;
-
-
import com.amaker.test.R;
-
-
public class TestBitmapActivity extends Activity {
-
-
private ImageView myImageView;
-
-
@Override
-
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);
-
- }
- }
/Chapter03_Resource/res/drawable
布局资源
/Chapter03_Resource/src/com/amaker/ch03/layout/TestLayoutActivity.java
- 代码
-
-
package com.amaker.ch03.layout;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.TextView;
-
import com.amaker.test.R;
-
-
public class TestLayoutActivity extends Activity {
-
private TextView myTextView;
-
private EditText myEditText;
-
private Button myButton;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.test_layout);
-
- myTextView = (TextView)findViewById(R.id.layoutTextView01);
- myEditText = (EditText)findViewById(R.id.layoutEditText01);
- myButton = (Button)findViewById(R.id.layoutButton01);
-
- }
- }
/Chapter03_Resource/res/layout/test_layout.xml
- 代码
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
-
-
-
<TableLayout
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:stretchColumns="1">
-
<TableRow>
-
<TextView
-
android:text="测试Layout:"
-
android:id="@+id/layoutTextView01"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textColor="@color/red_bg"/>
-
-
-
-
<EditText
-
android:text=""
-
android:id="@+id/layoutEditText01"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"/>
-
</TableRow>
-
-
<TableRow android:gravity="right">
-
<Button
-
android:text="Test"
-
android:id="@+id/layoutButton01"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
/>
-
</TableRow>
-
</TableLayout>
-
</LinearLayout>
菜单资源
/Chapter03_Resource/src/com/amaker/ch03/menu/TestMenuActivity.java
- 代码
-
-
package com.amaker.ch03.menu;
-
-
import android.app.Activity;
-
import android.app.AlertDialog;
-
import android.content.DialogInterface;
-
import android.os.Bundle;
-
import android.view.Menu;
-
import android.view.MenuInflater;
-
import android.view.MenuItem;
-
-
import com.amaker.test.R;
-
-
public class TestMenuActivity extends Activity {
-
private MenuInflater mi;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.test_menu);
-
mi = new MenuInflater(this);
- }
-
-
-
-
-
public boolean onCreateOptionsMenu(Menu menu) {
- mi.inflate(R.menu.file_menu, menu);
-
return true;
- }
-
-
@Override
-
public boolean onOptionsItemSelected(MenuItem item) {
-
switch (item.getItemId()) {
-
case R.id.about:
-
aboutAlert("本实例演示的是如何使用XML菜单资源来定义菜单!");
-
break;
-
case R.id.exit:
-
exitAlert("真的要退出吗?");
-
break;
- }
-
return true;
- }
-
-
-
private void exitAlert(String msg){
-
AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage(msg)
-
.setCancelable(false)
-
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int id) {
- finish();
- }
-
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int id) {
-
return;
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
- }
-
-
-
private void aboutAlert(String msg){
-
AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage(msg)
-
.setCancelable(false)
-
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int id) {
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
- }
-
- }
/Chapter03_Resource/res/layout/test_menu.xml
- 代码
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
-
<TextView
-
android:text="测试菜单资源"
-
android:id="@+id/menuTextView01"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"></TextView>
-
-
</LinearLayout>
字符串资源
/Chapter03_Resource/src/com/amaker/ch03/string/TestStringActivity.java
- 代码
-
-
-
package com.amaker.ch03.string;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.TextView;
-
-
import com.amaker.test.R;
-
-
public class TestStringActivity extends Activity {
-
private TextView myTextView;
-
@Override
-
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);
- }
- }
/Chapter03_Resource/res/layout/test_string.xml
- 代码
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical" android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
<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"
-
/>
-
</LinearLayout>
xml资源
- 代码
-
- package com.amaker.ch03.xml;
-
- import java.io.IOException;
-
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
-
- import android.app.Activity;
- import android.content.res.Resources;
- import android.content.res.XmlResourceParser;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
-
- import com.amaker.test.R;
-
- public class TestXmlActivity extends Activity {
- private TextView myTextView;
- private Button myButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test_xml);
-
-
myTextView = (TextView)findViewById(R.id.xmlContentTextView01);
-
myButton = (Button)findViewById(R.id.xmltTestButton01);
-
- myButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
int counter = 0;
-
StringBuilder sb = new StringBuilder("");
-
Resources r = getResources();
-
XmlResourceParser xrp = r.getXml(R.xml.test);
- try {
- while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
- if (xrp.getEventType() == XmlResourceParser.START_TAG) {
-
String name = xrp.getName();
- if(name.equals("customer")){
- counter++;
- sb.append("第"+counter+"条客户信息:"+"\n");
- sb.append(xrp.getAttributeValue(0)+"\n");
- sb.append(xrp.getAttributeValue(1)+"\n");
- sb.append(xrp.getAttributeValue(2)+"\n");
- sb.append(xrp.getAttributeValue(3)+"\n\n");
- }
- } else if (xrp.getEventType() == XmlPullParser.END_TAG) {
- } else if (xrp.getEventType() == XmlPullParser.TEXT) {
- }
- xrp.next();
- }
- myTextView.setText(sb.toString());
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- 代码
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical" android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
-
<Button
-
android:text="获得XML内容"
-
android:id="@+id/xmltTestButton01"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"></Button>
-
-
-
<TextView
-
android:text=""
-
android:id="@+id/xmlContentTextView01"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
/>
-
-
</LinearLayout>
本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080710