Android 底栏菜单 onclick 操作
我希望每个页面的应用程序都有一个通用的底部菜单栏。我设计了底部栏,但我对菜单图标的 onClick 事件感到困惑。我是否必须为每个活动类编写 onClick 侦听器的代码以使栏可见并在每个页面中工作,或者是否有任何其他方式我可以创建一个位于每个页面的公共底部栏,而无需在每个页面中编写菜单代码活动课。
我尝试通过创建一个基类并在其他子类中扩展它来创建, 正如 dave.c 在帖子 Android 创建底部栏菜单 中所述,但它不起作用为我。 请建议。谢谢。
I want to have a common bottom menu bar through out the applications with every page. I have designed the bottom bar but i am confused with onClick event of the menu icons. whether i have to write the code for onClick listener with every activity class to make bar visible and working in every page or if there is any other way i can create a common bottom bar that lies with every page without writing the code of menu in every activity class.
I tried to create through a creating a base class and extending it in other child classes,
as stated by dave.c in the post Android creating Bottom Bar Menu but it didnt work for me.
Please suggest. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单示例(如 dave.c 建议的那样):
您的 MyFirstActivity 将如下所示:
您的 MySecondActivity 活动:
在 my_first_activity.xml 布局中,您包括:
在 my_second_activity.xml 中:
在 Bottom_bar 中,您定义带有 onClick 处理程序的按钮:
使用此设计可能会遇到问题。例如,当您想在某些活动中使用 ListView 并希望子类化 ListActivity(TabActivity 是另一个示例)时,这是不可能的。
另一种方法是子类化 Activity 并定义一个处理 onClick 事件的通用处理程序。在这种情况下,您需要在每个活动中定义 onClick 处理程序并调用相应的公共处理程序的方法。
另一种方法是使用 TabHost 和 TabActivity。
Simple example (as dave.c suggested):
Your MyFirstActivity will look like:
Your MySecondActivity activity:
In my_first_activity.xml layout you include:
In my_second_activity.xml:
In bottom_bar you define buttons with onClick handlers:
You might run into problems using this design. For example when you want to use ListView in some of your activities and you want to subclass ListActivity (TabActivity is another example) it will be not possible.
Another way is to subclass Activity and define one common handler that handles onClick events. In this case you will need to define onClick handlers in each activity and call corresponding common handler's methods.
Yet another way is to use TabHost and TabActivity.