如何将NativeActivity的内容视图设置为用Java创建的组件 [英] How to set content view of NativeActivity to component created in Java

查看:290
本文介绍了如何将NativeActivity的内容视图设置为用Java创建的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将NativeActivity的内容设置为用Java创建的组件(例如FrameLayout,ImageView等)?我需要使用NativeActivity(用于在Xperia Play上获取触摸板输入),但是我还需要能够查看用Java代码创建的组件.

Is there a way to set the content of a NativeActivity to a component created in Java (such as a FrameLayout, ImageView, etc)? I have a need to use a NativeActivity (for getting touchpad input on Xperia Play), but I also need to be able to view components created in the Java code.

我尝试覆盖onCreate命令并在那里调用setContentView,但是尽管这不会引发任何错误,但是它也不执行任何操作(显然是因为本机代码已经设置了内容视图).是否有某种窗口ID或我可以传递给本机代码的内容,以告诉它内容视图应该是什么,或者可以在XML中以某种方式进行设置,以将NativeActivity的内容视图设置为我可以修改的组件来自Java?

I've tried overriding the onCreate command and calling setContentView there, but although that doesn't throw any errors, it doesn't do anything either (apparently because the content view is already set by the native code). Is there some sort of window id or something that I could pass to the native code to tell it what the content view should be, or can this somehow be set up in the XML to set the NativeActivity's content view to a component that I can modify from Java?

让我知道是否需要更多信息.

Let me know if you need any more information.

推荐答案

我将在此处为有相同问题的其他人提供答案,因为我最终确实找到了一种可行的方法.

I'll post the answer here for others with the same problem, since I did eventually figure out a method that works.

在onCreate()方法中,如果要设置任何特定的窗口标志(FEATURE_NO_TITLE,FLAG_FULLSCREEN,FLAG_KEEP_SCREEN_ON等),请在调用super.onCreate()之前执行这些操作(否则它们将被忽略).然后,通常将其放置在任何地方:

In the onCreate() method, if you are setting any particular window flags (FEATURE_NO_TITLE, FLAG_FULLSCREEN, FLAG_KEEP_SCREEN_ON, etc) do those before you call super.onCreate() (or they'll be ignored). Then wherever you would normally put this:

setContentView( whatever );

相反,请执行以下操作:

Do this instead:

getWindow().takeSurface( null );
getWindow().setContentView( whatever );

这是从Java端控制内容的基本方法.在代码中处理内容的任何地方,请使用"getWindow()"代替"this".

That's the basic way to get the content to be controlled from the Java side. Any place in your code where you deal with the content, use "getWindow()" instead of "this".

还有一些需要记住的事情,就是不会调用普通的onKey和onTouch方法(它们的本机等效项将被调用),因此,如果您需要能够在Java端处理输入,则可以必须建立一些JNI链接以将信息从本地发送到Java.我相信XPeria Play触摸板官方示例代码中包含了所有其他内容(AndroidManifest.xml的新增内容以及其他内容).

Some other things to keep in mind, are that the normal onKey and onTouch methods will not be called (their native equivalents will be instead), so if you need to be able to process input on the Java side, you'll have to set up some JNI linkage to send the information from native to Java. I believe everything else is included in the official XPeria Play touchpad sample code (the additions to the AndroidManifest.xml and what-not).

如果要查看我的项目以供参考,它是开源的,可以在以下位置找到:

If you want to look at my project for reference, it is open-source and can be found at:

http://www.paulscode.com/forum/index.php?topic = 75.msg1540#msg1540

只需单击"XPeria Play触摸板示例"旁边的源代码"链接.但是,这可能对您没有太大帮助,因为这是一个相当大的项目,可能很难找到您想要的东西.如果您有任何问题,请在我的论坛上发一个问题或给我发电子邮件,我将很乐意为您提供帮助.

Just click the "source code" link next to "XPeria Play Touch-Pad Example". It may not be all that useful to you though, because it is a rather large project and may be difficult to find what you are looking for. If you have any problems, post a question on my forum there or email me, and I'll be happy to help.

这篇关于如何将NativeActivity的内容视图设置为用Java创建的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆