您的位置:首页 > 移动开发 > Android开发

android中的资源文件

2016-07-05 22:18 330 查看

1.android中的提供了很多资源文件,资源文件放在res/下,或者将raw/下的放在assets下也行。有这些类型:都是xml文件

animator/ 属性动画
anim/ 视图动画
color/ a state list of colors 由颜色值对应一个状态
drawable/ 图片,shape,Animation drawables,Nine-Patches
mipmap/ 图片
layout/ 布局文件
menu/ 菜单文件
raw/ 保留文件的原始格式 ,音视频,Resources.openRawResource()
values/ arrays.xml,colors.xml,dimens.xml,strings.xml,styles.xml
xml/ Resources.getXML(). xml形式的配置文件

2.也可以对这些资源上做一些限定,例如不同的语言环境对应的values,不同的分辨率图片对应的drawable等待。

限定符:作用就是在对应的设备环境下使用对应的资源
MCC and MNC ,mcc310 mcc310-mnc004
Language and region ,en
Layout Direction ,ldrtl ldltr(默认)
smallestWidth ,sw320dp
Available width ,w720dp
Available height ,h720dp
Screen size:

small 320x426 dp
normal
320x470 dp
large
480x640 dp
xlarge
720x960 dp
Screen aspect
long,notlong
Round screen
round,notround
Screen orientation
port,land
Night mode ight,notnight
Screen pixel density (dpi)

ldpi 120dpi.
mdpi 160dpi.
hdpi 240dpi.
xhdpi
320dpi.
xxhdpi 480dpi.
xxxhdpi 640dpi.
nodpi 对bitmap resources 不缩放
tvdpi 213dpi
anydpi 不考虑dpi ,vector drawables.
There is a 3:4:6:8:12:16 scaling ratio between the six primary densities (
Touchscreen type notouch,finger(触摸屏)

3.资源访问,使用id即可。R.drawable.my_background_image,R.string.hello_message

4,资源变更:

<span style="font-size:14px;">@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}</span>

5.具体资源:

a.Animation Resources:

Property Animation有Animator, ValueAnimator, ObjectAnimator, or AnimatorSet.
View Animation有Tween animation:<set><alpha><scale><translate><rotate>
Frame animation :<animation-list>

b.Color State List Resource:

<selector>

c.Drawable Resources

有Bitmap File, Creates a BitmapDrawable.
有Nine-Patch File, Creates a NinePatchDrawable.
有Layer List ,Creates a LayerDrawable.
有State List,Creates a StateListDrawable.
有Level List,Creates a LevelListDrawable.
有Transition Drawable,Creates a TransitionDrawable.
有Inset Drawable,Clip Drawable,Scale Drawable,Shape Drawable

d.Layout Resource:

<menu>
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.example_menu, menu);

e.String Resources

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
使用html标签, <string name="welcome">Welcome to <b>Android</b>!</string>
使用Spannables

f.Style Resource

使用style来定义Theme,控件样式
<style
name="style_name"
parent="@[package:]style/style_to_inherit">
<item
name="[package:]style_property_name"
>style_value</item>
</style>

g.More Resource Types

Bool
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<bool
name="bool_name"
>[true | false]</bool>
</resources></span>

Color
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<color
name="color_name"
>hex_color</color>
</resources></span>
Dimension:dp,sp,px
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name="dimension_name"
>dimension</dimen>
</resources></span>

ID
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources></span>

Integer
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<integer
name="integer_name"
>integer</integer>
</resources> </span>
Integer Array
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array
name="integer_array_name">
<item
>integer</item>
</integer-array>
</resources> </span>
Typed Array
这个在自定义控件中,获取自定义属性时我们用到过
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>@drawable/home</item>
<item>@drawable/settings</item>
<item>@drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);
</span>

6.manifest文件:

Manifest的结构
<?xml version="1.0" encoding="utf-8"?>

<manifest>

<uses-permission />
<permission />
<permission-tree />
<permission-group />
<instrumentation />
<uses-sdk />
<uses-configuration />
<uses-feature />
<supports-screens />
<compatible-screens />
<supports-gl-texture />

<application>

<activity>
<intent-filter>
<action />
<category />
<data />
</intent-filter>
<meta-data />
</activity>

<activity-alias>
<intent-filter> . . . </intent-filter>
<meta-data />
</activity-alias>

<service>
<intent-filter> . . . </intent-filter>
<meta-data/>
</service>

<receiver>
<intent-filter> . . . </intent-filter>
<meta-data />
</receiver>

<provider>
<grant-uri-permission />
<meta-data />
<path-permission />
</provider>

<uses-library />

</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: