第14/15讲- Android资源管理

14/15讲 Android资源管理

Android中的资源是指非代码部分,比如图片、MP3,字符串,XML文件等。在一个android工程中,res和assets是用来保存资源文件的。

resassets的区别

1.res中的资源可以通过R资源类访问。

res中包含各种文件夹,对资源进行分类。(比较常用)

anim(xml动画文件),drawable(图片), layout(布局文件), menu(菜单), raw(二进制文件), values(常量值), xml

2. assets中保存不进行编译加工的原始文件,例如MP3文件,Android程序不能通过R类直接访问。必须通过AssetManager类以二进制流的形式来读取。

使用资源的两种一般方式

1.   在代码中使用context的getResources()方法得到Resources对象,该对象提供了获得各种资源的方法;

2.   在其他资源中引用资源的一般格式:

(1)      @[包名称:]资源类型/资源名称

(2)      R.资源类型.资源名称

一、 颜色资源

颜色值的定义是通过RGB三原色来定义的。

资源位置

res/values/colors.xml

获得颜色方法

Resources.getColor()

引用资源格式

Java代码中:R.color.color_name

XML文件中:@color/color_name

颜色XML文件格式

<?xml version="1.0" encoding="utf-8"?>

<resources>

<color name="color_name">#color_value</color>

</resources>

<?xml version="1.0"encoding="utf-8" ?>

<resources>

<color name="red">#FF0000</color><!--红色 -->

<color name="orange">#FFA500</color><!--橙色 -->

<colorname="yellow">#FFFF00</color><!--黄色 -->

<color name="green">#008000</color><!--绿色 -->

<color name="cyan">#00FFFF</color><!--青色 -->

<color name="blue">#0000FF</color><!--蓝色 -->

<color name="purple">#800080</color><!--紫色 -->

 <color name="black">#000000</color><!--黑色 -->

<color name="white">#FFFFFF</color><!--白色 -->

<color name="gold">#FFD700</color><!--金色 -->

<color name="skyblue">#87CEEB</color><!--天蓝色 -->

<color name="gray">#808080</color><!--灰色 -->

</resources>

首先在res中values下建立colors.xml文件

<?xml version="1.0"encoding="utf-8"?>

<resources>

<color name="bg_color">#ff0000</color>     

<color name="test_color">#00ff00</color>

</resources>

颜色资源的使用:

(a)      在XML文件中

在布局文件layout中使用:android:textColor="@color/test_color"

全局layout中背景颜色android:background="@color/bg_color"

(b)      在java代码中

在布局文件中添加一个button,为此button添加android:onClick="test"。

在MainActivity.java中使用

public void test(View view){

int color = this.getResources().getColor(R.color.bg_color);

Toast.makeText(this, ""+color, 1).show();

this.getWindow().setBackgroundDrawableResource(R.color.bg_color);

}

二、字符串资源

资源位置

res/values/strings.xml

获得string方法

Context.getString()

引用资源格式

Java代码中:R.string.string_name

XML文件中:@ string/string_name

颜色XML文件格式

<resources>

<string name=" string _name"> string _value</string>

</resources>

首先在res中values下strings.xml文件

<?xml version="1.0"encoding="utf-8"?>

<resources>

<string name="welcome">欢迎,-_-</string>

</resources>

颜色资源的使用:

(a )在XML文件中

在布局文件layout中使用:android:text="@string/welcome"

(b )在java代码中

在布局文件中添加一个button,为此button添加 android:onClick="test"。

在MainActivity.java中使用

public voidtest(View view){

String str = this.getString(R.string.welcome);

Toast.makeText(this,str, 1).show();

Button button =(Button) findViewById(R.id.button1);

button.setText(R.string.welcome);

}

三、尺寸资源

可以使用一些常用的尺寸单位来定义一些文字尺寸,试图组件的宽和高等。

尺寸资源是一个数字类型的数据,被定义在 res/values/dimens.xml文件中。

单位表示

单位名称

说明

px

像素

屏幕真是像素

in

英寸

基于屏幕的物理英寸

dp

与密度无关的像素

相对屏幕物理密度的抽象单位

sp

与比例无关的像素

资源位置

res/values/dimens.xml

获得尺寸资源的方法

getResources().getDimension(   )

引用资源格式

Java代码中:R. dimen. dimen_name

XML文件中:@dimen/dimen_name

XML文件格式

<resources>

<dimen name=" dimen _name"> dimen _value</dimen>

</resources>

 在dimen.xml中

   <dimen name="text_width">150px</dimen>

   <dimen name="text_height">100px</dimen>

   <dimen name="button_width">30mm</dimen>

   <dimen name="button_height">10mm</dimen>

在activity_main.xml中

android:width="@dimen/text_width"

android:height="@dimen/text_height"

android:width="@dimen/button_width"

android:height="@dimen/button_height"

android:onClick="test"

在Main_Activity.java中

         publicvoid test(View view){

                   Buttonbutton = (Button) findViewById(R.id.button1);

                   floatwidth=this.getResources().getDimension(R.dimen.text_width);

                   floatheight=this.getResources().getDimension(R.dimen.text_height);

                   button.setWidth((int)width);

                   button.setHeight((int)height);

         }

四、使用原始的XML数据

Android允许将任意的xml文件用作资源。放在resxml子目录下

资源位置

resxml

获得尺寸资源的方法

getResources().getXml(   )

引用资源格式

Java代码中:R. xml. xml_name

XML文件中:@ xml/xml _name

XML文件格式

<?xml version="1.0" encoding="utf-8"?>

<resources>

<someElement name=value/>

<someElement name=value></someElement>

</resources>

 在res文件夹中新建一个xml文件夹,在此文件夹下新建一个users.xml文件,如下

<?xml version="1.0"encoding="utf-8"?>

<resources>

   <user name="Liu" phone="18070510481"></user>

<user name="Gong" phone="182333666636"/>

</resources>

新建一个Button,并为之添加android:onClick="test"

在MainActivity.java文件中编写

public voidtest(View view){

                   Stringtext = "";

                         //根据XML资源的ID获取解析该资源的解析器

                //XmlResourceParser是XmlPullParser的子类

                   XmlResourceParserxrp = this.getResources().getXml(R.xml.users);              //获取xml

                   try{

                            while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){  //是否到XML文档的结尾处

                                     if(xrp.getEventType()==XmlResourceParser.START_TAG){               //如果遇到了开始标签

                                               Stringtagname=xrp.getName();                                                    //获取该标签的标签名

                                               if(tagname.equals("user")){

                                                        Stringuname = xrp.getAttributeValue(0);                //根据属性索引来获取属性值

                                                        Stringphone = xrp.getAttributeValue(1);

                                                        text+="姓名:"+uname+";电话:"+phone+"; ";

                                               }

                                     }

                                     xrp.next();                  //获取解析器的下一个事件

                            }

TextView textView = (TextView) findViewById(R.id.textView1);

                        textView.setText(text);

                   }

                   catch(XmlPullParserExceptione){   e.printStackTrace();          }

catch(IOException e){      e.printStackTrace();          }

}

五、图片资源

资源位置

res/drawable/file_name.png /file_name.jpg / file_name.gif

获得尺寸资源的方法

Resources.get Drawable()

引用资源格式

Java代码中:R.drawable.file_name

XML文件中:@[package:]drawable/file_name

drawable资源是一些图片或者颜色资源,主要用来绘制屏幕。

drawable资源分为三类:Bitmap File(位图文件)、ColorDrawable(颜色)、Nine-Patch Image(九片图片)

这里只讲述常用的位图文件的使用。

Android中支持的位图文件包括:png, jpg,gif

Android不允许图片资源的文件名中出现大写字母,且不能以数字开头

Drawabledrawable=this.getResources().getDrawable(R.drawable.a1);

this.getWindow().setBackgroundDrawable(drawable);

也可以直接在layout中进行背景设置

android:background="@drawable/a1"

原文地址:https://www.cnblogs.com/anyuan9/p/6171606.html