Android ActionBar: androidactionbar

Source: Internet
Author: User

Android ActionBar: androidactionbar

1. Guide, examples, personal feelings

First on the official website guide link http://developer.android.com/guide/topics/ui/actionbar.html

Refer to the examples on the official website http://developer.android.com/samples/ActionBarCompat-Basic/index.html

At first glance, the guide shows that ActionBar is similar to include heder.html, which is commonly used in web development. For example, the first line of the cnblog page.

 

2. compatibility problems: sdk 11 and above can be used directly, sdk 11 and below need support-v7

PS: the two types of configuration files must be different as follows. Note the following: # Use the ActionBar configuration that comes with sdk 11.

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:support="http://schemas.android.com/apk/res-auto"# xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.example.actionbar.MainActivity" >    <item        android:id="@+id/action_settings"        android:orderInCategory="100"        android:title="@string/action_settings"        support:showAsAction="never"        #app:showAsAction="never"/>        <item        android:id="@+id/menu_refresh"        android:icon="@drawable/ic_action_refresh"        android:title="@string/menu_refresh"        support:showAsAction="ifRoom"        />        

 

3. Constructor

There are configuration files and two types of building in the program code

@ Override public boolean onCreateOptionsMenu (Menu menu ){
Build getMenuInflater () in the configuration file (). inflate (R. menu. main, menu); MenuItem locationItem = menu. add (0, R. id. menu_location, 0, R. string. menu_location );

Program build locationItem. setIcon (R. drawable. ic_action_location); MenuItemCompat. setShowAsAction (locationItem, MenuItem. SHOW_AS_ACTION_IF_ROOM); return true ;}

 

4. Full code

package com.example.actionbar;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.os.Build;import android.os.Bundle;import android.support.v4.view.MenuItemCompat;import android.support.v7.app.ActionBar;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;//@SuppressLint("NewApi")public class MainActivity extends ActionBarActivity {        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.sample_main);   // android.app.ActionBar actionBar =  getActionBar();    //actionBar.hide();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.main, menu);        MenuItem locationItem = menu.add(0,R.id.menu_location,0,R.string.menu_location);        locationItem.setIcon(R.drawable.ic_action_location);        MenuItemCompat.setShowAsAction(locationItem, MenuItem.SHOW_AS_ACTION_IF_ROOM);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        switch (item.getItemId()) {        case R.id.menu_refresh:            // Here we might start a background refresh task            return true;        case R.id.menu_location:            // Here we might call LocationManager.requestLocationUpdates()            return true;        case R.id.menu_settings:            // Here we would open up our settings activity            return true;        case R.id.action_settings:            // Here we would open up our settings activity            return true;    }        return super.onOptionsItemSelected(item);    }}

5. Follow-up .....

 

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.