是否可以使用 findViewById(R.layout.my_layout)
在我的原生 Android 实现中查找 View ?
如果不是,我如何获取对我创建并在调用我的 Android native 接口(interface)时返回的 native View 的引用?
public android.widget.TextView createNativeView() {
android.widget.TextView dynamicTextView = new android.widget.TextView(AndroidNativeUtil.getContext());
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
dynamicTextView.setText(" Hello World ");
dynamicTextView.setGravity(android.view.Gravity.CENTER);
return dynamicTextView;
}
在我的主应用程序中,我正在创建对等组件,如下所示:
TestNativeInterface n = TestNativeInterface)NativeLookup.create(TestNativeInterface.class);
PeerComponent view = n.createNativeView();
最佳答案
在代号一中不需要它。如果你想添加文本组件,你可以使用 add(view);
(或者 add(CENTER, view);
如果它是一个 BorderLayout
).
请注意,这显然只能在 Android 设备上运行,因此要使其正常运行,您需要类似以下内容:
Form myForm = new Form("Native", new BorderLayout());
TestNativeInterface n = TestNativeInterface)NativeLookup.create(TestNativeInterface.class);
if(n != null && n.isSupported()) {
PeerComponent view = n.createNativeView();
myForm.add(CENTER, view);
}
myForm.show();
我选择使用 BorderLayout
的原因是它在计算大小方面具有确定性。计算 View 的首选大小可能需要一段时间,因此布局可能会错误地放置它,但 BorderLayout
的 CENTER
位置会将其拉伸(stretch)到 表格
.
关于android - findViewById(R.layout.my_layout) 在 Android Native 实现中可用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48188790/