android 常用方法

‌1 getIdentifier是安卓开发一个非常有用的方法,它属于Context类,用于根据资源的名称和类型动态获取资源的id,常用于跨模块和获取系统的资源id等。

vbnet 复制代码
/**
 * 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.)
 */
@Discouraged(message = "Use of this function is discouraged because resource reflection makes "
                     + "it harder to perform build optimizations and compile-time "
                     + "verification of code. It is much more efficient to retrieve "
                     + "resources by identifier (e.g. `R.foo.bar`) than by name (e.g. "
                     + "`getIdentifier("bar", "foo", null)`).")
public int getIdentifier(String name, String defType, String defPackage) {
    return mResourcesImpl.getIdentifier(name, defType, defPackage);
}

name:资源的名称,即在res目录下的资源文件的名称,例如"id_test" defType:资源的类型,例如drawable,layout,string等。 defPackage 资源所在的包名。 如果找不到对应的资源,此方法会返回0。

ini 复制代码
val resourceId = resources.getIdentifier("id_test", "string", packageName)
if (resourceId !== 0) {
    val textString = resources.getString(resourceId)
    Log.d("zhangqing", "[onCreate] resourceId=$textString")
} else {
    // 资源不存在
    Log.d("zhangqing", "[onCreate] resourceId no found")
}
xml 复制代码
<resources>
    <string name="app_name">KotlinDemoTest</string>
    <string name="id_test">我爱安卓</string>
</resources>

另外

ini 复制代码
val getResourceName = resources.getResourceName(resourceId)
val getResourceEntryName = resources.getResourceEntryName(resourceId)
val getResourcePackageName = resources.getResourcePackageName(resourceId)
val getResourceTypeName = resources.getResourceTypeName(resourceId)

Log.d("zhangqing", "[onCreate] getResourceName=$getResourceName")
Log.d("zhangqing", "[onCreate] getResourceEntryName=$getResourceEntryName")
Log.d("zhangqing", "[onCreate] getResourcePackageName=$getResourcePackageName")
Log.d("zhangqing", "[onCreate] getResourceTypeName=$getResourceTypeName")

有时我们在一个应用程序中访问另一个应用程序的资源或上下文,这时我们需要使用createPackageContext()方法,这个方法允许我们获取其他应用的上下文,并且使用这个上下文访问其资源。

相关推荐
恋猫de小郭1 小时前
再次紧急修复,Flutter 针对 WebView 无法点击问题增加新的快速修复
android·前端·flutter
李慕婉学姐1 小时前
【开题答辩过程】以《基于Android的健康助手APP的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
android·java·mysql
似霰3 小时前
传统 Hal 开发笔记6----App 访问硬件服务
android·framework·hal
爱装代码的小瓶子4 小时前
【c++知识铺子】封装map和set(详细版)
android·java·c++
私人珍藏库4 小时前
AutoGLM无需豆包手机,让AI自动帮你点外卖-刷视频
android·ai·智能手机·工具·软件·辅助·autoglm
孤舟簔笠翁5 小时前
【Android驱动14】Android系统Crash工具使用方法和分析
android
帅得不敢出门6 小时前
MTK Android11 APP调用OTA升级
android·java·开发语言·framework
2501_915909066 小时前
苹果应用加密方案的一种方法,在没有源码的前提下,如何处理 IPA 的安全问题
android·安全·ios·小程序·uni-app·iphone·webview
用户2018792831676 小时前
Android App 换肤原理:用 "装修小房子" 故事浅谈
android