Adding Action Buttons

为简单起见,此处只保留了关键代码。详细过程参见官方教程:Adding Action Buttons

在AndroidManifest.xml文件中,我们有如下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simon.myapplication"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
        <activity
            android:name="com.example.simon.myapplication.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.simon.myapplication.DisplayMessageActivity"
            android:label="@string/title_activity_actions"
            android:parentActivityName="com.example.simon.myapplication.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.simon.myapplication.MainActivity" />
        </activity>
    </application>
</manifest>

此处,为第二个Acitivity(DisplayMessageActivity)指定了parentAcitivity。

在res/menu/main_activity_actions.xml文件中,我们定义menu如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="ifRoom" />
    <!-- Overflow, should appear as action button -->
    <item android:id="@+id/action_overflow"
        android:icon="@drawable/ic_action_overflow"
        android:title="@string/action_overflow"
        app:showAsAction="always" />
</menu>

此处,我们将Search设置为ifRoom(空间足够时显示),Overflow设置为always(总是显示)。

在DisplayMessageActivity类中,有如下代码:

 1 public class DisplayMessageActivity extends ActionBarActivity {
 2
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6
 7         // Get the message from the intent
 8         Intent intent = getIntent();
 9         String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
10
11         // Create the text view
12         TextView textView = new TextView(this);
13         textView.setTextSize(40);
14         textView.setText(message);
15
16         // Set the text view as the activity layout
17         setContentView(textView);
18
19         ActionBar actionBar = getSupportActionBar();
20         actionBar.setHomeAsUpIndicator( R.drawable.ic_launcher );
21         actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
22                 | ActionBar.DISPLAY_SHOW_TITLE );
23     }
24
25     @Override
26     public boolean onCreateOptionsMenu(Menu menu) {
27         // Inflate the menu; this adds items to the action bar if it is present.
28         getMenuInflater().inflate(R.menu.main_activity_actions, menu);
29         return true;
30     }
31
32     @Override
33     public boolean onOptionsItemSelected(MenuItem item) {
34         // Handle presses on the action bar items
35         switch (item.getItemId()) {
36             case R.id.action_search:
37                 Log.d( "MyApp", "action_search" );
38                 return true;
39             case R.id.action_overflow:
40                 Log.d( "MyApp", "action_overflow" );
41                 return true;
42             default:
43                 return super.onOptionsItemSelected(item);
44         }
45
46     }
47 }

注:1) 在onCreateOptionsMenu函数中,将main_activity_actions.xml定义的action添加到action bar中。

2) 在onOptionsItemSelected函数中,定义响应action bar中的某个action被选中时的代码。

3) 19~22行(onCreate函数中),用于将默认的左键头替换成R.drawable.ic_launcher图标,即,使用图标代替默认的左键头,作为返回上级页面的按钮。

效果图如下:

时间: 2024-10-06 23:09:18

Adding Action Buttons的相关文章

Android中文翻译 - Adding the Action Bar 添加活动栏(action bar)

2014-10-28 张云飞VIR 翻译自:https://developer.android.com/training/basics/actionbar/index.html 添加活动栏(Adding the Action Bar) 译者注:我找不到更好的词汇翻译action bar,虽然我也认为 活动栏 不是个好的翻译,但总要有个中文名字.不过为了方便识别,本文仍继续使用英文的actionbar 活动栏action bar 是非常重要的设计元素之一,你可以为你的app中的activity来实

Android设计和开发系列第二篇:Action Bar(Develop—Training)

Adding the Action Bar GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.1 or higher YOU SHOULD ALSO READ Action Bar Implementing Effective Navigation DESIGN GUIDE Action Bar The action bar is one of the most important design elements you can imple

Android官方入门文档[6]添加Action按钮

Android官方入门文档[6]添加Action按钮 Adding Action Buttons添加Action按钮 This lesson teaches you to1.Specify the Actions in XML2.Add the Actions to the Action Bar3.Respond to Action Buttons4.Add Up Button for Low-level Activities You should also read?Providing Up

【Android文档】Training-------Adding the Action Bar

转载请注明出处 :http://blog.csdn.net/chziroy/article/details/44171197 本文主要翻译android开发官网Training中关于ActionBar的几篇文章 Setting up the Action Bar 在大多最基本的格式中,action bar都会呈现activity的标题以及在左侧呈现app的图标.它能使用户知道当前处于哪个activity,并且使该app有一个持久的标志.如下图 要使用action bar需要app的activit

android ActionBar(官方指导)

安装ActionBar: Support Android 3.0 and Above Only(仅支持3.0版本之后) <manifest ... > <uses-sdk android:minSdkVersion="11" ... /> ... </manifest> Support Android 2.1 and Above(如果想让应用支持3.1版本之后,需如下操作) 1安装v7 appcompat 库 2Update your activit

android官方教程从零开刷(三)

接下来就不能偷懒了,ActionBar很重要. Adding the Action Bar   Setting up the Action Bar 要设置一个ActionBar很容易,在android 3.0以上都会自动显示ActionBar,但是在3.0一下的版本就需要使用兼容包才能使用ActionBar Adding libraries without resources Make sure you have downloaded the Android Support Library us

ActionBar 的简单使用

About ActionBar The action bar is one of the most important design elements you can implement for your app's activities. It provides several user interface features that make your app immediately familiar to users by offering consistency between othe

Apple Watch应用开发

参考转载:http://www.swiftkiller.com/?p=613 A.概览 一.概览--开始为 Apple Watch 进行开发 -1.第三方应用需要两个不同的可执行文件:在Apple Watch上运行的Watch应用(Watch应用只包含与应用程序的用户界面有关的storyboards和资源文件),    在用户iPhone上运行的WatchKit应用扩展(WatchKit应用扩展则包含了用于管理.监听应用程序的用户界面以及响应用户交互的代码). Watch应用需要尽可能实现App

Android文档 学习目录

Building Your First App After you've installed the Android SDK, start with this class to learn the basics about Android app development. Creating an Android Project Running Your Application Building a Simple User Interface Starting Another Activity A