173

クラスの1つにあるメソッドにリソースIDを渡す必要があります。参照が指すIDと文字列の両方を使用する必要があります。これをどのように達成するのが最善ですか?

例えば:

R.drawable.icon

この整数IDを取得する必要がありますが、文字列「icon」にもアクセスする必要があります。

メソッドに渡さなければならないのは「アイコン」文字列だけであることが望ましいでしょう。

4

14 に答える 14

189

@EboMike: それが存在することを知りませんでしResources.getIdentifier()た。

私のプロジェクトでは、次のコードを使用してそれを行いました。

public static int getResId(String resName, Class<?> c) {

    try {
        Field idField = c.getDeclaredField(resName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    } 
}

R.drawable.iconリソース整数値の値を取得するために、このように使用されます

int resID = getResId("icon", R.drawable.class); // or other resource class

Resources.getIdentifier()私が行ったようにリフレクションを使用するよりも遅いというブログ投稿を見つけました。それをチェックしてください

于 2010-12-13T11:21:40.553 に答える
93

この関数を使用して、リソース ID を取得できます。

public static int getResourceId(String pVariableName, String pResourcename, String pPackageName) 
{
    try {
        return getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    } 
}

したがって、このような描画可能な呼び出し関数を取得したい場合

getResourceId("myIcon", "drawable", getPackageName());

文字列の場合、次のように呼び出すことができます

getResourceId("myAppName", "string", getPackageName());

これを読む

于 2013-09-30T11:48:06.897 に答える
40

これは@Macarseの回答に基づいています。

これを使用して、より高速でコードに適した方法でリソース ID を取得します。

public static int getId(String resourceName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(resourceName);
        return idField.getInt(idField);
    } catch (Exception e) {
        throw new RuntimeException("No resource ID found for: "
                + resourceName + " / " + c, e);
    }
}

例:

getId("icon", R.drawable.class);
于 2013-07-12T19:12:52.330 に答える
30

リソース名からアプリケーションリソース IDを取得する方法は、非常によくある質問であり、十分に回答されています。

リソース名からネイティブの Androidリソース IDを取得する方法については、あまり回答がありません。リソース名でAndroidドローアブルリソースを取得する私のソリューションは次のとおりです。

public static Drawable getAndroidDrawable(String pDrawableName){
    int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", "android");
    if(resourceId==0){
        return null;
    } else {
        return Resources.getSystem().getDrawable(resourceId);
    }
}

メソッドを変更して、他のタイプのリソースにアクセスできます。

于 2012-04-12T03:10:18.700 に答える
12

文字列と整数をペアにする必要がある場合は、マップはどうですか?

static Map<String, Integer> icons = new HashMap<String, Integer>();

static {
    icons.add("icon1", R.drawable.icon);
    icons.add("icon2", R.drawable.othericon);
    icons.add("someicon", R.drawable.whatever);
}
于 2011-05-14T21:08:16.933 に答える
10

私はこれが好きでした、それは私のために働いています:

    imageView.setImageResource(context.getResources().
         getIdentifier("drawable/apple", null, context.getPackageName()));
于 2013-07-26T13:49:08.400 に答える
9

を使用できますResources.getIdentifier()が、XML ファイルで使用する文字列の形式を使用する必要がありますpackage:drawable/icon

于 2010-12-13T09:58:45.467 に答える
6

文字列からリソース ID を取得する簡単な方法。ここで、 resourceNameは、XML ファイルにも含まれる drawable フォルダー内のリソース ImageView の名前です。

int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable",
context.getPackageName());
im.setImageResource(id);
于 2015-04-27T17:48:33.117 に答える
1

String リソース名から Drawable id を取得するために、次のコードを使用しています。

private int getResId(String resName) {
    int defId = -1;
    try {
        Field f = R.drawable.class.getDeclaredField(resName);
        Field def = R.drawable.class.getDeclaredField("transparent_flag");
        defId = def.getInt(null);
        return f.getInt(null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        return defId;
    }
}
于 2016-06-29T10:41:50.473 に答える
1

MonoDroid / Xamarin.Android では、次のことができます。

 var resourceId = Resources.GetIdentifier("icon", "drawable", PackageName);

しかし、GetIdentifier は Android では推奨されていないため、次のように Reflection を使用できます。

 var resourceId = (int)typeof(Resource.Drawable).GetField("icon").GetValue(null);

try/catch を配置するか、渡す文字列を検証することをお勧めします。

于 2016-05-27T17:03:20.203 に答える