最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中實(shí)現(xiàn)根據(jù)資源名獲取資源ID

 更新時(shí)間:2015年01月16日 13:18:37   投稿:junjie  
這篇文章主要介紹了Android中實(shí)現(xiàn)根據(jù)資源名獲取資源ID,本文講解了使用文件名獲取資源ID的方法,需要的朋友可以參考下

接觸過(guò)Android開(kāi)發(fā)的同學(xué)們都知道在Android中訪問(wèn)程序資源基本都是通過(guò)資源ID來(lái)訪問(wèn)。這樣開(kāi)發(fā)起來(lái)很簡(jiǎn)單,并且可以不去考慮各種分辨率,語(yǔ)言等不同資源顯式指定。

痛點(diǎn)

但是,有時(shí)候也會(huì)有一些問(wèn)題,比如我們根據(jù)服務(wù)器端的值取圖片,但是服務(wù)器端絕對(duì)不會(huì)返回給我們的是資源id,最多是一種和文件名相關(guān)聯(lián)的值,操作資源少的時(shí)候,可以維護(hù)一個(gè)容器進(jìn)行值與資源ID的映射,但是多的話,就需要另想辦法了。

便捷的方法

在這種情況下,使用文件名來(lái)得到資源ID顯得事半功倍。 通過(guò)調(diào)用Resources的getIdentifier可以很輕松地得到資源ID。 幾個(gè)簡(jiǎn)單的示例:

復(fù)制代碼 代碼如下:

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);

運(yùn)行結(jié)果

復(fù)制代碼 代碼如下:

I/MainActivity( 4537): testGetResourceIds imageResId = 2130837504;imageResIdByAnotherForm = 2130837504;musicResId=2130968576;notFoundResId =0

看一看API

直接API

1.這個(gè)方法用來(lái)使用資源名來(lái)獲取資源ID
2.完整的資源名為package:type/entry,如果資源名這個(gè)參數(shù)有完整地指定,后面的defType和defPackage可以省略。
3.defType和defPackage省略時(shí),需要將其設(shè)置成null
4.注意這個(gè)方法不提倡,因?yàn)橹苯油ㄟ^(guò)資源ID訪問(wèn)資源會(huì)更加效率高
5.如果資源沒(méi)有找到,返回0,在Android資源ID中0不是合法的資源ID。

復(fù)制代碼 代碼如下:

**
     * 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

實(shí)際上上述API調(diào)用的是AssetManager.class中的native方法。

復(fù)制代碼 代碼如下:

/**
     * Retrieve the resource identifier for the given resource name.
     */
    /*package*/ native final int getResourceIdentifier(String type,
                                                       String name,
                                                       String defPackage);

相關(guān)文章

  • Android的HTTP類庫(kù)Volley入門(mén)學(xué)習(xí)教程

    Android的HTTP類庫(kù)Volley入門(mén)學(xué)習(xí)教程

    這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)框架Volley的入門(mén)學(xué)習(xí)教程,Volley適合于輕量級(jí)的通信功能開(kāi)發(fā),善于處理JSON對(duì)象,需要的朋友可以參考下
    2016-02-02
  • Mac Android Studio安裝圖文教程

    Mac Android Studio安裝圖文教程

    本文主要介紹了Mac Android Studio安裝教程,文中通過(guò)圖文代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android模仿知乎的回答詳情頁(yè)的動(dòng)畫(huà)效果

    Android模仿知乎的回答詳情頁(yè)的動(dòng)畫(huà)效果

    這篇文章主要介紹了Android模仿“知乎”的回答詳情頁(yè)的動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android APP與媒體存儲(chǔ)服務(wù)的交互

    Android APP與媒體存儲(chǔ)服務(wù)的交互

    本文介紹如何在 Android 中,開(kāi)發(fā)者的 APP 如何使用媒體存儲(chǔ)服務(wù)(包含MediaScanner、MediaProvider以及媒體信息解析等部分),包括如何把 APP 新增或修改的文件更新到媒體數(shù)據(jù)庫(kù)、如何在多媒體應(yīng)用中隱藏 APP 產(chǎn)生的文件、如何監(jiān)聽(tīng)媒體數(shù)據(jù)庫(kù)的變化等等。
    2013-10-10
  • Android實(shí)現(xiàn)文件下載

    Android實(shí)現(xiàn)文件下載

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Flutter系列重學(xué)Container示例詳解

    Flutter系列重學(xué)Container示例詳解

    這篇文章主要為大家介紹了Flutter系列重學(xué)Container示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android中斷并重啟一個(gè)Thread線程的簡(jiǎn)單方法

    Android中斷并重啟一個(gè)Thread線程的簡(jiǎn)單方法

    下面小編就為大家?guī)?lái)一篇Android中斷并重啟一個(gè)Thread線程的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Android 自定義View實(shí)現(xiàn)任意布局的RadioGroup效果

    Android 自定義View實(shí)現(xiàn)任意布局的RadioGroup效果

    這篇文章主要介紹了Android 自定義View實(shí)現(xiàn)任意布局的RadioGroup,需要的朋友可以參考下
    2018-11-11
  • Android Studio 設(shè)置代碼提示和代碼自動(dòng)補(bǔ)全快捷鍵方式

    Android Studio 設(shè)置代碼提示和代碼自動(dòng)補(bǔ)全快捷鍵方式

    這篇文章主要介紹了Android Studio 設(shè)置代碼提示和代碼自動(dòng)補(bǔ)全快捷鍵方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android 常用log 關(guān)鍵字

    Android 常用log 關(guān)鍵字

    本文給大家分享android常用的log關(guān)鍵字,感興趣的朋友一起看看吧
    2017-09-09

最新評(píng)論

铁岭市| 应城市| 花莲县| 黑河市| 临潭县| 桐乡市| 增城市| 巴马| 阿鲁科尔沁旗| 扎鲁特旗| 甘孜县| 天津市| 鄂托克旗| 平阳县| 太仓市| 泽普县| 离岛区| 合水县| 榆中县| 天柱县| 甘洛县| 哈尔滨市| 新龙县| 万荣县| 建水县| 屏山县| 玉环县| 张家口市| 阳泉市| 三穗县| 福海县| 澎湖县| 原阳县| 琼中| 文水县| 榆林市| 成都市| 成安县| 桂阳县| 漯河市| 绥德县|