android 根据资源名称,如何在Android中按名称访问可绘制资源
生活随笔
收集整理的這篇文章主要介紹了
android 根据资源名称,如何在Android中按名称访问可绘制资源
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
你可以做這樣的事情。
public static Drawable getDrawable(String name) {
Context context = YourApplication.getContext();
int resourceId = context.getResources().getIdentifier(name, "drawable", YourApplication.getContext().getPackageName());
return context.getResources().getDrawable(resourceId);
}
為了從任何地方訪問上下文,您可以擴展Application類。
public class YourApplication extends Application {
private static YourApplication instance;
public YourApplication() {
instance = this;
}
public static Context getContext() {
return instance;
}
}
并將其映射到您的Manifest application標簽中
android:name=".YourApplication"
....
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android 根据资源名称,如何在Android中按名称访问可绘制资源的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贷款的基本要求有哪些
- 下一篇: android sina oauth2.