Android development-ActionBar and androidactionbar

Source: Internet
Author: User

Android development-ActionBar and androidactionbar

The little friends who use the APP are certain to have an impression on the ActionBar. Today, we will help you achieve the following results.

The first step is to open our development tool. Here I am using the Eclipse + ADT plug-in, and then create our project. Here we select the minimum Android version 3.0 or later.

Then start our "Plagiarism". First open us. We can see that the top part of the title is divided into the left and right parts, and the left side is, the search button + more button is displayed on the right. Click the search button to display a text input box. Click More to display the hidden menu, which includes adding friends, initiating group chat, scanning, and payment. Now that we have a design framework, we will start our development.

First open our project and there is an onCreateOptionsMenu method in the MainActivity. java file. This method is to initialize the method for creating the menu. We can see that the default reference is the main. xml file. By default, the system generates a settings button for us. Next we will modify this file and add our menu to it:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@+id/search"        android:showAsAction="ifRoom|collapseActionView"        android:actionViewClass="android.widget.SearchView"        android:icon="@drawable/ic_menu_search"        android:title="@string/action_search"/>    <item        android:id="@+id/addFriend"        android:icon="@drawable/ic_menu_rotate"        android:title="@string/menu_addFrideds"/>        <item        android:id="@+id/teamChart"        android:icon="@drawable/ic_menu_refresh"        android:title="@string/menu_teamChart"/>        <item        android:id="@+id/monery"        android:icon="@drawable/ic_menu_preferences"        android:title="@string/menu_getMonery"/>        <item        android:id="@+id/look"        android:icon="@drawable/ic_menu_save"        android:title="@string/menu_look"/></menu>

To write this file, I also need to open a file named strings. xml in our res --> values folder to configure our Chinese constants.

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> manyi </string> <string name = "action_search"> Search </string> <string name = "action_more"> more </string> <string name = "menu_addFrideds"> Add a friend </string> <string name = "menu_teamChart"> initiate a group chat </string> <string name = "menu_getMonery"> collection </string> <string name = "menu_look"> scan </string> <string name = "welcome"> hello, manyi </string> </resources>

Now we are running our program. We can see a similar effect on the top of our program, with the application icon + application name on the left, there is a search button + on the right to indicate more buttons. Now we will replace the more icons that are provided by the system by default with a Customized button. Open our AndroidManifest. xml, we will find that the system applies a style file android: theme = "@ style/AppTheme" by default, click to open this style file, add our custom more icons to the style:

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <!--        Base application theme for API 14+. This theme completely replaces        AppBaseTheme from BOTH res/values/styles.xml and        res/values-v11/styles.xml on API 14+ devices.    -->    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">        <!-- API 14 theme customizations can go here. -->        <item name="android:actionOverflowButtonStyle">@style/menuOverflowButtonStyle</item>    </style>        <style name="menuOverflowButtonStyle">        <item name="android:src">@drawable/ic_menu_more</item>    </style></resources>

Now, when we run our project, we will find that the effect is still a little different. This is what we need to do through the MainActivity. in the java file, 1 sets the display of more custom icons through the reflection mechanism, 2 overrides the onMenuOpened method to set each menu as shown in the form of an icon and a title.

Package com. example. androidmenuview; import java. lang. reflect. field; import java. lang. reflect. method; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. viewConfiguration; import android. view. window; import android. widget. toast; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceSta Te) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); setMenuOverflowAlways (); getActionBar (). setDisplayShowHomeEnabled (false); // set the ActionBar application icon to not display} // initialize Menu @ Override public boolean onCreateOptionsMenu (Menu menu) {menu. add (Menu. NONE, Menu. FIRST + 6, 7, "new "). setIcon (android. r. drawable. ic_input_add); // manually add the menu getMenuInflater (). inflate (R. menu. main, menu); return true ;} // Add the Menu Click Event @ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. search: // Toast. makeText (this, "Search button", Toast. LENGTH_SHORT ). show (); break; case R. id. addFriend: // Toast. makeText (this, "Add friends", Toast. LENGTH_SHORT ). show (); break; case R. id. teamChart: // Toast. makeText (this, "group chat", Toast. LENGTH_SHORT ). show (); break; case R. id. look: // Toast. makeText (this, "Scan", Toast. LENGTH_SHORT ). show (); break; case R. id. monery: // Toast. makeText (this, "Collection", Toast. LENGTH_SHORT ). show (); break; case Menu. FIRST + 6: // Toast. makeText (this, "new", Toast. LENGTH_SHORT ). show (); break;} Toast. makeText (this, item. getTitle (), Toast. LENGTH_SHORT ). show (); return super. onOptionsItemSelected (item);} // set the first icon in the menu to display in the upper right corner of the title --- use the reflection mechanism to complete public void setMenuOverflowAlways () {try {ViewConfiguration config = ViewConfiguration. get (this); Field field = ViewConfiguration. class. getDeclaredField ("sHasPermanentMenuKey"); field. setAccessible (true); field. setBoolean (config, false);} catch (Exception e) {e. printStackTrace () ;}}// set each Menu to be displayed as the right title of the left icon @ Override public boolean onMenuOpened (int featureId, Menu menu) {if (featureId = Window. FEATURE_ACTION_BAR & menu! = Null) {if (menu. getClass (). getSimpleName (). equals ("MenuBuilder") {try {Method = menu. getClass (). getDeclaredMethod ("setOptionalIconsVisible", Boolean. TYPE); method. setAccessible (true); method. invoke (menu, true);} catch (Exception e) {e. printStackTrace () ;}} return super. onMenuOpened (featureId, menu );}}

Here I have written the onOptionsItemSelected () method to add click events for each menu.

Now, we will share with you the implementation of the menu at the top of the app.

 

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.