Android implements the method of adding menu for tab

This article describes the method of adding menu for tab in Android implementation. Share with you for your reference, as follows:

In Android, there is no menu related method in the tabactivity class, so if you use tabactivity alone for development, you cannot create menu items. To add a menu to the tab, there are two methods:

1. The main event inherits the activity. In this way, the oncreateoptionsmenu method can be directly overridden in the main event to add a menu to the program. If each tab needs to have a different menu, you can add an ontachangedlistener for the tabhost, and modify a global variable (such as intmenunnumber) in this listener. In this way, you can add different menus for different tabs by switching () on this variable when oncreateoptionsmenu.

Method advantages: if all tabs share a menu, this method is very simple.

Method disadvantages: the main event class is very large; If each tab wants to use a different menu, the steps will be cumbersome.

2. The main event inherits tabactivity, and then you need to use the tab of menu to create a new event inherited from activity. When calling, you can use setcontent (New intent(). SetClass (this, newactivity. Class)) method. In this way, each newly created activity can override the oncreateoptionsmenu method, and the adding process will be very flexible.

Method advantages: it is very easy to develop a separate menu for each tab, and the development process is more flexible.

Method disadvantages: the data communication between tabs is complex.

For more Android related content, readers who are interested can view the special topics of this site: introduction and advanced tutorial of Android development, activity operation skills summary of Android programming, Android resource operation skills summary, Android file operation skills summary, Android SQLite database operation skills summary, Android JSON format data operation skills summary Summary of Android database operation skills, summary of SD card operation methods for Android programming development, summary of Android view skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>