布局在 Android 中的自定义包
custom package with layout in Android
我创建了一个自定义包 com.test1.web 并且我在这个包中有一个 class。 (Class activity 扩展)
我的应用程序 运行 与 com.test2.main 来源。
我无法访问自定义包中的 R.layout。
我怎样才能访问它?
您可以在扩展应用程序中创建 RemoveViews 并通过 intent 将其发送到主包。
Intent intent = new Intent("OPEN_YOUR_MAIN_APP");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_layout);
intent.putExtra("layout", views);
startActivity(intent);
或者另一种方法是通过方法 Context#createPackageContext(String, int) 在主应用程序中创建扩展应用程序的上下文
并调用 Context#getResources()
try {
Context extensionAppContext = context.createPackageContext("com.test1.web", Context.CONTEXT_RESTRICTED);
Resources extensionAppResources = extensionAppContext.getResources();
int layoutId = extensionAppResources.getIdentifier("layout_name", "layout", "com.test1.web");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
我创建了一个自定义包 com.test1.web 并且我在这个包中有一个 class。 (Class activity 扩展)
我的应用程序 运行 与 com.test2.main 来源。
我无法访问自定义包中的 R.layout。
我怎样才能访问它?
您可以在扩展应用程序中创建 RemoveViews 并通过 intent 将其发送到主包。
Intent intent = new Intent("OPEN_YOUR_MAIN_APP");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_layout);
intent.putExtra("layout", views);
startActivity(intent);
或者另一种方法是通过方法 Context#createPackageContext(String, int) 在主应用程序中创建扩展应用程序的上下文 并调用 Context#getResources()
try {
Context extensionAppContext = context.createPackageContext("com.test1.web", Context.CONTEXT_RESTRICTED);
Resources extensionAppResources = extensionAppContext.getResources();
int layoutId = extensionAppResources.getIdentifier("layout_name", "layout", "com.test1.web");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}