接触过Android开发的同学们都知道在Android中访问程序资源基本都是通过资源ID来访问。这样开发起来很简单,并且可以不去考虑各种分辨率,语言等不同资源显式指定。
但是,有时候也会有一些问题,比如我们根据服务器端的值取图片,但是服务器端绝对不会返回给我们的是资源id,最多是一种和文件名相关联的值,操作资源少的时候,可以维护一个容器进行值与资源ID的映射,但是多的话,就需要另想办法了。
在这种情况下,使用文件名来得到资源ID显得事半功倍。 通过调用Resources的getIdentifier可以很轻松地得到资源ID。 几个简单的示例
lineos:false
1 2 3 4 5 6 7 8 9 10 11 12 13 | Resources res = getResources(); final String packageName = getPackageName(); int imageResId = res.getIdentifier("ic_launcher", "drawable", packageName); int imageResIdByAnotherForm = res.getIdentifier(packageName + ":drawable/ic_launcher", null, null); int musicResId = res.getIdentifier("test", "raw", packageName); int notFoundResId = res.getIdentifier("activity_main", "drawable", packageName); Log.i(LOGTAG, "testGetResourceIds imageResId = " + imageResId + ";imageResIdByAnotherForm = " + imageResIdByAnotherForm + ";musicResId=" + musicResId + ";notFoundResId =" + notFoundResId); |
---|
运行结果
lineos:false
1 | I/MainActivity( 4537): testGetResourceIds imageResId = 2130837504;imageResIdByAnotherForm = 2130837504;musicResId=2130968576;notFoundResId =0 |
---|
package:type/entry
,如果资源名这个参数有完整地指定,后面的defType和defPackage可以省略。lineos:false android.content.res.Resources.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * Return a resource identifier for the given resource name. A fully * qualified resource name is of the form "package:type/entry". The first * two components (package and type) are optional if defType and * defPackage, respectively, are specified here. * * <p>Note: use of this function is discouraged. It is much more * efficient to retrieve resources by identifier than by name. * * @param name The name of the desired resource. * @param defType Optional default resource type to find, if "type/" is * not included in the name. Can be null to require an * explicit type. * @param defPackage Optional default package to find, if "package:" is * not included in the name. Can be null to require an * explicit package. * * @return int The associated resource identifier. Returns 0 if no such * resource was found. (0 is not a valid resource ID.) */ public int getIdentifier(String name, String defType, String defPackage) { try { return Integer.parseInt(name); } catch (Exception e) { // Ignore } return mAssets.getResourceIdentifier(name, defType, defPackage); } |
---|
实际上上述API调用的是AssetManager.class中的native方法。
lineos:false
1 2 3 4 5 6 | /** * Retrieve the resource identifier for the given resource name. */ /*package*/ native final int getResourceIdentifier(String type, String name, String defPackage); |
---|
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有