クラスの1つにあるメソッドにリソースIDを渡す必要があります。参照が指すIDと文字列の両方を使用する必要があります。これをどのように達成するのが最善ですか?
例えば:
R.drawable.icon
この整数IDを取得する必要がありますが、文字列「icon」にもアクセスする必要があります。
メソッドに渡さなければならないのは「アイコン」文字列だけであることが望ましいでしょう。
クラスの1つにあるメソッドにリソースIDを渡す必要があります。参照が指すIDと文字列の両方を使用する必要があります。これをどのように達成するのが最善ですか?
例えば:
R.drawable.icon
この整数IDを取得する必要がありますが、文字列「icon」にもアクセスする必要があります。
メソッドに渡さなければならないのは「アイコン」文字列だけであることが望ましいでしょう。
@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()
私が行ったようにリフレクションを使用するよりも遅いというブログ投稿を見つけました。それをチェックしてください。
この関数を使用して、リソース 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());
これは@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);
リソース名からアプリケーションリソース 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);
}
}
メソッドを変更して、他のタイプのリソースにアクセスできます。
文字列と整数をペアにする必要がある場合は、マップはどうですか?
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);
}
私はこれが好きでした、それは私のために働いています:
imageView.setImageResource(context.getResources().
getIdentifier("drawable/apple", null, context.getPackageName()));
を使用できますResources.getIdentifier()
が、XML ファイルで使用する文字列の形式を使用する必要がありますpackage:drawable/icon
。
文字列からリソース 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);
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;
}
}
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 を配置するか、渡す文字列を検証することをお勧めします。