Display of Actionbar and overflow in Android

Source: Internet
Author: User

Transferred from: http://www.sxt.cn/u/756/blog/4386

Recently, using Android's API documentation to learn about the use of Actionbar in Android, the most basic form of Action bar is to display a title for the activity and an app icon to the left of the title. In this simple form, action Bar is useful for informing users of their current location and maintaining the same identity for your app for all activity. The Action Bar is a new navigation bar feature that, after Android 3.0, is added to the system's API, which identifies the location of the user's current operating interface and provides additional user actions, interface navigation, and other functions. The advantage of using Actionbar is that it can provide a global unified UI interface.

Configuration Implementation Actionbar

First you need to configure the menu yourself, in the menu to add the corresponding item (/RES/MENU/MAIN.XML) in the following configuration:, added three item

123456789101112131415 <item    android:id="@+id/action_settings"    android:icon="@drawable/ic_launcher"    android:showAsAction="always|withText"    android:title="@string/action_settings"/><item    android:id="@+id/icon_edit"    android:icon="@drawable/ic_launcher"    android:showAsAction="never|withText"    android:title="@string/main_actionEdit"/><item    android:id="@+id/icon_search"    android:icon="@drawable/ic_launcher"    android:showAsAction="never|withText"    android:title="@string/main_actionSearch"/>

The Showasaction attribute in item has four:
1. Always: This value causes the menu item to be displayed on the action bar.
2. Ifroom: If there is enough space, this value causes the menu item to appear on the action bar.
3. Never: This value causes the menu item to never appear on the action bar.
4. Withtext: This value causes the menu item to be displayed along with its icon and the text of the menus.

You also need to rewrite the Oncreateoptionsmenu method in the activity:

123456 @Override  publicboolean onCreateOptionsMenu(Menu menu) {      MenuInflater inflater = getMenuInflater();      inflater.inflate(R.menu.main, menu);      returnsuper.onCreateOptionsMenu(menu);  }

The results of the API documentation should look like this, noting three points:

But the reality is:

Because I am a beginner, so when the official documents and personal combat conflict, it is depressed, is a step by step according to the document, so this time is depressed, the Internet is a hard find, found the reason:

Http://developer.android.com/guide/topics/ui/actionbar.html, there is a paragraph in English:

The Action Bar provides users access to the most important action items relating to the app's current context. Those that appear directly in the Action bar with an icon and/or text is known as action buttons. Actions that can ' t fit in the action bar or aren ' t important enough is hidden in the action overflow. The user can reveal a list of the other actions by pressing the overflow button on the right side (or the device Menu butt On, if available).

I will not translate, simply say if the device has a menu function keys, the function of showing hidden actions will be implemented by the menu function keys, click on the menu, the effect is as follows:

Menu appeared, but there is no three points, or a little reconciled, and looked for, and someone has encountered this problem, for those three point menu, Android 3.0 (API level 11) + standard name is called: Action overflow button low version (Android 2.3.x (API level) or lower) corresponding to the name: Overflow menu, the effect (not three points) is the more button.

If you want to show overflow menu, need to manually write code to load out, StackOverflow have a foreigner paste code, OnCreate call on it, I can not:


123456789101112 private void getOverflowMenu() {      try {         ViewConfiguration config = ViewConfiguration.get(this);         Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");         if(menuKeyField != null) {             menuKeyField.setAccessible(true);             menuKeyField.setBoolean(config, false);         }     } catch (Exception e) {         e.printStackTrace();     } }


Hide Actionbar, use Getactionbar to get directly hidden after the line, it does not map:

12 ActionBar bar=getActionBar();bar.hide();

You can also set the Click event for item in menu at this time:

12345678910111213141516171819 @Override   public boolean onOptionsItemSelected(MenuItem item) {       switch (item.getItemId()) {       case R.id.icon_edit:          Toast.makeText(this, "触发编辑按钮事件", Toast.LENGTH_SHORT).show();           return true;       case R.id.action_settings:           Intent intent=new Intent(this,PesonActivity.class);              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP                        | Intent.FLAG_ACTIVITY_NEW_TASK);             startActivity(intent);           return true;       case R.id.icon_search:           Toast.makeText(this, "触发搜索按钮事件", Toast.LENGTH_SHORT).show();           return true;       default:           return super.onOptionsItemSelected(item);   }   }
Manually implement Menu

Activity can be called from one another, or you can return from the child activity to the parent activity, create a new personactivity, and manually write the code to configure the menu:

12345678910111213 @Overridepublic boolean onCreateOptionsMenu(Menu menu) {    super.onCreateOptionsMenu(menu);    //添加菜单项    MenuItem add=menu.add(0,0,0,"添加");    MenuItem del=menu.add(0,0,0,"删除");    add.setIcon(R.drawable.btn_check_on_pressed);    del.setIcon(R.drawable.btn_close_selected);    //绑定到ActionBar      add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);    del.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);    return true;}


This time you can see the red Place has a return arrow that needs to be configured in the activity:

1 android:parentActivityName="com.example.googleaction.MainActivity"

A method needs to be called in the activity of pesonactivity:

1 getActionBar().setDisplayHomeAsUpEnabled(true);

Display of Actionbar and overflow in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.