在已经扩展了 ActionBarActivity 的主活动中扩展一个附加类?
我正在尝试添加 Google“ActionBarCompat”示例代码中找到的相同操作栏兼容性 到我的一个项目。
我的问题是我已经在主类中扩展了 MapActivity,但是 ActionBarCompat 的主类似乎要求您扩展 ActionBarActivity。
这是我的 Java 101 问题:我对 Java 有足够的了解,知道我不能拥有多重继承,但不足以了解处理这种情况的理想方法。我的解决方案是在主类中扩展 ActionBarActivity,然后让 ActionBarActivity
扩展 MapActivity
。这是执行此操作的正确方法吗?如果是这样,如果我希望它扩展其他东西,我是否必须制作一个重复的 ActionBarActivity
?
感谢您考虑这个问题!
I am attempting to add the same action bar compatibility found in the Google "ActionBarCompat" sample code to one of my projects.
My problem is that I already extend MapActivity
in my main class, but the main class of ActionBarCompat seems to require that you extend ActionBarActivity
.
Here's my Java 101 question: I know enough about Java to know that I can't have multiple inheritance, but not enough to know the ideal way to handle this situation. My solution was to extend ActionBarActivity in my main class, but then make ActionBarActivity
extend MapActivity
. Is this the correct way to do this? If so, wouldn't I have to make a duplicate ActionBarActivity
if I want it to extend other things?
Thanks for your consideration of this question!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是一种完全有效的方法。如果您想要一个不扩展 MapActivity 的 ActionBarActivity 类,则必须创建另一个类。我不认为这会导致大量的代码重复(我猜这就是你所害怕的)。 Android 开发人员感到羞耻,因为他们让您需要编写自己的 ActionBarHelper 类。
Yes, that's a totally valid way of doing this. If you want an ActionBarActivity class that doesn't extend the MapActivity, you'll have to create another class. I don't think this will lead to a lot of code-duplication (which I'm guessing is what you're afraid of). Shame on the android developers though for making you need to code your own ActionBarHelper class.
查看您提供的示例 - ActionBarActivity 仅具有 ActionBarHelper 字段并调用其上的方法。尝试在 MapActivity 中执行相同的操作。
Look at the example you provided - ActionBarActivity is only having an ActionBarHelper field and calling methods on it. Try doin the same in you MapActivity.
使用“Peter Knego”的解决方案,并链接到此库:
https://github.com/ lvillani/android-compat-actionbar
或者如果您使用 Eclipse 和 svn :
https://svn.github.com/lvillani/android-compat-actionbar
Use "Peter Knego"'s solution, and link to this library:
https://github.com/lvillani/android-compat-actionbar
or if your using Eclipse and svn :
https://svn.github.com/lvillani/android-compat-actionbar