Android中资源文件小结

参考自:感谢大佬们的帮助
https://droidyue.com/blog/2014/09/12/get-resource-id-by-name-in-android/
https://blog.csdn.net/mingli198611/article/details/7105850

一.XML文件中@,@+,?,@*的含义以及区别

1.@
  1. 类型一:(引用自定义资源)
    形如android:xx = "@[package:]type/name",自定义package:省略
    例如
    android:text="@string/app_name"
    android:textColor="@color/gary"
  1. 类型二:(引用系统资源)
    形如android:xx = "@android:type/name",为@[package:]type/name的一个子类
    例如
    android:textColor="@android:color/white"
2.@* 引用系统所有资源

形如android:xx = "@*android:type/name"

注意:android:xx = "@android:type/name"只能调用系统的public属性资源

3.? 引用主题属性-无需具体值,会在主题属性中查找并替换

只能在XML文件或style资源中使用。

形如?[namespace:]type/name,这里类型可选。
例如android:textColor="?attr/colorAccent"

4.@引用资源 @+创建资源 形如:@+type/name
  • 代表在R文件中新添加一个静态final常量值
--@+id/name 代表创建一个名为name的资源标识引用
--@id/name 引用名为name的资源标识引用(如果资源存在)
--@android:id/name 引用系统name资源

例如

android:id="@+id/tv_test" 代表创建一个tv_test的资源标识引用
android:id="@id/tv_test" 引用一个名为tv_test的资源

二.引用/获取资源文件的值

1.传统且常见的使用方式(根据资源ID获取资源值)

通过getResource.getXXX()获取值。

例如

	    resources = getResources();
        packageName = getPackageName();
        Log.e(TAG, "packageName:" + packageName);
        //常用-正常获取资源
        //通过资源ID获取字符串
        String str = resources.getString(R.string.app_name);
        //通过资源ID获取颜色值
        int colorBlue = resources.getColor(R.color.blue);
        Log.e(TAG, "colorBlue:" + colorBlue);
        String []array = resources.getStringArray(R.array.str_array);
        Log.e(TAG, "array:" + array.length);
        Drawable drawable = resources.getDrawable(R.drawable.point_bg_checked);
2.特殊情况(通过资源名获取资源ID,在通过资源ID设置相对应的值)

//通过getIdentifier(name,type,package)

偶尔我们通过网络请求获取到资源名称,然后设置相应的资源值;这时候就使用第二种方式。

例如

        int icLauncherId = resources.getIdentifier("ic_launcher_background", "drawable", packageName);
        //name完整
        int anotherIcLauncherId = resources.getIdentifier(packageName+":drawable/ic_launcher_background", null, null);
        Log.e(TAG, "icLauncherId:" + icLauncherId);
        Log.e(TAG, "anotherIcLauncherId:" + anotherIcLauncherId);
        int colorId = resources.getIdentifier("colorAccent", "color", packageName);
        Log.e(TAG, "colorId:" + colorId);

查看源码

 /**
     * 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) {
        return mResourcesImpl.getIdentifier(name, defType, defPackage);
    }

它的三个参数name,defType,defPackage分别对应了本文中第一部分的内容。

参数xml文件
namename
defTypetype
defPackagepackage

@[package:]type/name.

注意

  1. 获取资源ID为0时,说明不存在。
  2. 此方法并不高效。
  3. 见案例;当name完整时,后面两个参数直接填null即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值