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

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

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

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

痛點

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

便捷的方法

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

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

運行結(jié)果

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

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

看一看API

直接API

1.這個方法用來使用資源名來獲取資源ID
2.完整的資源名為package:type/entry,如果資源名這個參數(shù)有完整地指定,后面的defType和defPackage可以省略。
3.defType和defPackage省略時,需要將其設(shè)置成null
4.注意這個方法不提倡,因為直接通過資源ID訪問資源會更加效率高
5.如果資源沒有找到,返回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

實際上上述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類庫Volley入門學(xué)習(xí)教程

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

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

    Mac Android Studio安裝圖文教程

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

    Android模仿知乎的回答詳情頁的動畫效果

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

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

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

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

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

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

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

    Android中斷并重啟一個Thread線程的簡單方法

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

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

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

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

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

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

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

最新評論

深圳市| 通化市| 蒙城县| 保德县| 常德市| 徐闻县| 永清县| 河津市| 大洼县| 鄂温| 江孜县| 香河县| 伽师县| 沧州市| 阿城市| 盘锦市| 东乡族自治县| 丰县| 鄄城县| 广丰县| 张家港市| 汝州市| 开封县| 乐清市| 香格里拉县| 滦平县| 万山特区| 承德县| 名山县| 庐江县| 榆中县| 微山县| 大连市| 荥经县| 鸡东县| 华亭县| 理塘县| 新竹县| 新宁县| 长泰县| 长葛市|