Android 资源目录(Style Resource)介绍

来源:互联网 发布:海康威视的设备域名 编辑:程序博客网 时间:2024/05/16 10:28
Android资源目录(Style Resource)介绍

1.      介绍

资源文件一般放在res/*/目录下.res目录下的文件夹分为values/drawable等用于存放不同类型的资源,改文件夹根据资源类型/语言/分辨率/横竖屏等,命名规则为:

value                     en                        ldpi

drawable              en-rUS               mdpi

layout                    CN                       hdpi                    land(横屏)

xml                        zh-rTW        -       xhdpi        -         port(竖屏)

menu                    ru                         nodpi

raw                        ru_rRU               xxhdpi

……                      ……                     ……                    ......

Example:value-en-rUS-hdpi-land

当前语言是en-rUS,分辨率是hdpi,屏幕是横屏时调用此目录下资源.

 

2.      字符串values/

字符串资源放在res/values/*.xml文件中.文件名任意.不过一般都放在相应名称文件中,便于识别.

申明方法:

<resources>

</resources>

(1)    string 存放一个字符串值(默认存放文件strings.xml)

ex: <string name="app_name">TestRes</string>使用: getString(R.string.app_name);

(2)    string-array 存放一个string数组(默认存放文件strings.xml/arrays.xml)

ex: <string-arrayname="app_string_array">        <item> item1 </item>        <item> item2 </item>      </string-array>使用: Stringarray[] = getResources().getStringArray(R.array. app_string_array);

(3)    array 存放一个int数组(默认存放文件arrays.xml)

ex: <arrayname="app_array">        <item> 0 </item>        <item> 1 </item>      </array>使用: int array[]= getResources().getIntArray(R.array.app_array);

(4)    plurals  根据传进来的参数的大小来决定使用哪个字符串

ex: <pluralsname="app_plurals">        <itemquantity="zero">%d, -- 0-use zero</item>        <itemquantity="one">%d, -- 1-use one</item>        <itemquantity="two">%d, -- 2-use two</item>        <item quantity="few">%d,-- 3,4-use few</item>        <itemquantity="many">%d, -- 11-99-use many</item>        <itemquantity="other">%d, -- 0-use other</item></plurals>使用: Stringplural = getResources().getQuantityString(R.plurals.app_plurals, 3, 3);(第一个参数3表示当传入3时使用哪个字串,第二个参数3会赋值到字串的%d中)

(5)    dimen 存放一个尺寸相关的参数(默认存放文件:dimens.xml)

ex: <dimen name="app_dimen"> 20dip</dimen>/*关于尺寸单位后续整理*/使用:android:layout_width=”@dimen/app_dimen”     setWidth(R.dimen.app_dimen);

(6)    style  定义一个包含各种属性的对象(默认存放文件:style.xml)

通俗点说,就是将控件(比如textview)的属性都写在这个style对象中,这样可以多个控件复用.

ex: <resources>    <style name="CustomText"parent="@style/Text">        <itemname="android:textSize">20sp</item>        <itemname="android:textColor">#008</item>    </style></resources>使用: <EditText style="@style/CustomText"/>

(7)    color  定义一个包含color值的对象(默认存放文件color.xml)

ex: <color name="color_name"> #80ff0000</color>使用: android:textColor="@color/ color_name" int color =res.getColor(R.color. color_name);

(8)    bool/integer 定义一个bool/int类型对象,就不一一介绍了.

 

3.      图片资源drawable/

最简单的就是在drawable目录下定义放一个图片文件,like image.png

使用: android:drawable="@drawable/image"     getResources().getDrawable(R.drawable.image);

还可以定义一个img.xml文件,通过不同的state来决定使用不同的img.png

ex: <selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_enabled="true" android:state_active="false"android:state_focused="false"android:drawable="@drawable/ic_lockscreen_handle_mifavor_normal"/><itemandroid:state_enabled="true" android:state_active="true"android:state_focused="false"android:drawable="@drawable/ic_lockscreen_handle_mifavor_pressed"/></selector>


4.      布局layout/  xml/

布局资源放在res/layout(xml)/*.xml文件中

Layout/xml:  layout定义了一个activity的布局,

            使用方法: setContentView(R.layout.main_activity);

            xml通常是用来定义一个preference的布局,


5.      菜单 menu/

菜单资源放在 res/menu/*.xml文件中(菜单中各项含义后续介绍)

Ex:main.xml

<item       android:id="@+id/action_settings"        android:orderInCategory="100"        android:showAsAction="never"           android:title="@string/action_settings"/>使用方法: getMenuInflater().inflate(R.menu.main, menu);


6.      文件 raw/

文件资源放在res/raw/*文件中.

读取: public InputStream openRawResource (int id)


7.      动画  anim/

用来定义一个tween动画(alpha,scale,translate,rotate)的所有参数


8.      animator/

不懂


原创粉丝点击