我正在尝试整合 AndroidPDFViewer by barteksc使用 native 界面将此代码添加到我的 CodenameOne 应用程序中。我想创建 PDFView,将其作为 native 界面中的 View 返回,并在我的项目中将其用作 PeerComponent。当我在常规 CN1 代码中创建 Java 接口(interface)文件时,我的返回值是 PeerComponent 并且 native 接口(interface)生成器将该接口(interface)方法的返回值设置为 View
.
我之前在 Android Studio 中创建原生 Android 应用程序时使用过这个库,它运行良好。但是,我的 CN1 项目似乎无法找到 PDFView
的 xml 布局。 ,通常会在 Android xml 文件中。根据 GitHub 页面,这就是 xml 的样子:
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
使用 CN1 构建提示,我将以下内容添加到 .properties 文件中:
codename1.arg.android.cusom_layout1= <?xml version="1.0" encoding="utf-8"?><com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView" android:layout_width="match_parent" android:layout_height="match_parent"/>
在我的 Android native 界面中,我添加了以下代码(使用适当的导入,没有错误):
LayoutInflater li = LayoutInflater.from(com.codename1.impl.android.AndroidNativeUtil.getActivity().getApplicationContext());
View theview = li.inflate(R.layout.cusom_layout1, null, false);
PDFView pdfView = (PDFView)theview.findViewById(R.id.pdfView);
/*(PDFView)findViewById(R.id.pdfView);*/
if (param1) {
pdfView.fromBytes(param)
.pages(1)
.onError(onErrorListener)
.onTap(onTapListener)
.enableSwipe(false)
.enableDoubletap(false)
.load();
} else {
pdfView.fromBytes(param)
.onError(onErrorListener)
.onTap(onTapListener)
.enableSwipe(false)
.enableDoubletap(false)
.load();
}
return (View)pdfView;
我遇到了这个错误;看起来它找不到布局,但我不知道如何将布局与在 Java 代码中初始化 PDFView 的格式联系起来(根据文档,它需要引用 View ID)。
Exception: android.view.InflateException - Binary XML file line #1: Binary XML file line #1: Error inflating class PDFView
有什么想法吗? 谢谢!
最佳答案
为什么不直接通过代码手动创建 PDFView
而不是使用 XML 布局?
请注意,每个 XML 布局只是一个与组件层次结构相同的 View 层次结构,您可以使用从 AndroidNativeUtil
获取的上下文创建 PDFView
的实例。
关于android - 如何在 CodenameOne 原生界面中使用具有自定义布局的 Android 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842489/