The action bar allows you to add buttons for 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 are known as action buttons.
Actions that can't fit in the action bar or aren't important enough are hidden in the action overflow.

ActionBar允许你去添加非常重要的action菜单连接到应用的上下文,ActionBar中可以添加一个图标或者文本的按钮。Actions如果在ActionBar中放置不下则会隐藏。

Specify the Actions in XML

定义Actions在XML中

All action buttons and other items available in the action overflow are defined in an XML menu resource.
To add actions to the action bar, create a new XML file in your project's res/menu/ directory.

Add an <item> element for each item you want to include in the action bar. For example:

所有的按钮或者被隐藏的可被操作view都在菜单资源XML文件中定义,下面在你的工程的res/menu/directory下创建一个XML文件,给actionBar添加action

添加方法如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>

This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings
action should always appear in the overflow. (By default, all actions appear in the overflow, but it's good practice to explicitly declare your design intentions for each action.)

如果ActionBar的显示空间足够大,查询action就应该显示并可见,但是设置action应该总是隐藏的(默认,所有的action都是隐藏的,但是最好是明确你的每个aciton的设计意图)

The icon attribute
requires a resource ID for an image. The name that follows @drawable/ must
be the name of a bitmap image you've saved in your project's res/drawable/ directory.
For example,"@drawable/ic_action_search" refers
to ic_action_search.png.
Likewise, the title attribute
uses a string resource that's defined by an XML file in your project's res/values/ directory,
as discussed in Building
a Simple User Interface
.

icon属性需要指定一个图片资源ID,在@drawable/路径后的资源名必须是你已经在你工程res/drawable/目录下存在的图片名称。例如,“@drawable/ic_action_search"映射到资源ic_action_search.png.同样的,title属性必须使用res/values/目录下XML文件中定义好的string字符串,就如刚说的这样,来创建一个简单的界面。

If
your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsActionattribute
is not available from the android: namespace.
Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible
within the scope of the file in which you declare it.) For example:

如果你用的类库兼容Android2.1以下版本,那么Android命名空间下的showAsAction属性没有用,为了代替库文件提供的这个属性你必须定义你自己的XML命名空间并使用这个命名空间作为属性的前缀。(一个自定义的XML命名空间应该是你应用的名字,但是这个名字是可以任意取的,最好是有意义并可理解的名称
)如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="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"
yourapp:showAsAction="ifRoom" />
...
</menu>

Add the Actions to the Action Bar

给ActionBar添加Action

To
place the menu items into the action bar, implement the onCreateOptionsMenu() callback
method in your activity to inflate the menu resource into the given Menu object.
For example:

将菜单项添加到你的action bar,在你的activity中实现onCreateOptionsMenu()回调方法去映射菜单资源到指定的菜单项,例如:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

Respond to Action Buttons

Action 按钮监听回调

When the user
presses one of the action buttons or another item in the action overflow, the system calls your activity's onOptionsItemSelected() callback
method. In your implementation of this method, call getItemId()on
the given MenuItem to
determine which item was pressed—the returned ID matches the value you declared in the corresponding <item> element's android:id attribute.

当用户按下一个action按钮或者隐藏的其他菜单,系统将自动回调你的activity中的onOptionsItemSelected()方法。在你的方法实现中,调用getItemId()去得到相应的元素ID.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Add Up Button for Low-level Activities

给低优先级的Acitivity添加Up按钮

All screens in your app that are not the main entrance to your app (activities that are not the "home" screen) should offer the user a way to navigate to the logical parent screen in the app's hierarchy by pressing the Up button in the action bar.

When running on Android 4.1 (API level 16) or higher, or when usingActionBarActivity from
the Support Library, performing Upnavigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.

For example, here's how you can declare an activity's parent in the manifest:

在你的应用中不是所有的界面都是主界面,通过按下ActionBar中的Up按钮,应该提供给用户一个父视图。

当运行在Android4.1或者更高版本,或者添加高版本类库,提供顶部导航需要在mainfest文件中声明父activity.

例如,在mainfest中如何声明:

<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>

Then enable the app icon as the Up button
by calling setDisplayHomeAsUpEnabled():

然后通过调用setDisplayHomeAsUpEnable()是Up图标生效。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}

Because the system now knows MainActivity is
the parent activity for DisplayMessageActivity,
when the user presses the Up button,
the system navigates to the parent activity as appropriate—you do not need
to handle theUp button's
event.

Android官方教程翻译(6)——添加ActionBar的更多相关文章

  1. Android官方教程翻译(5)——设置ActionBar

    Setting Up the Action Bar 设置Action Bar PREVIOUSNEXT THIS LESSONTEACHES YOU TO 这节课教你 1.    Support An ...

  2. Android官方教程翻译(4)——启动另一个Activity

    Starting Another Activity 启动另一个Activity PREVIOUSNEXT THIS LESSON TEACHES YOU TO 这节课教你 1.   Respond t ...

  3. Android官方教程翻译(1)——创建第一个Android应用

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9822431 Building Your First App GETSTARTED ...

  4. Android官方教程翻译(3)——创建一个简单的用户界面

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9839523 Building a Simple User Interface 创建 ...

  5. Android官方教程翻译(2)——运行第一个程序

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9823623 Running Your App PREVIOUSNEXT THIS ...

  6. 【Android 开发教程】动态添加Fragments

    本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...

  7. c# MongoDB Driver 官方教程翻译

    先贴官方文档地址:http://mongodb.github.io/mongo-csharp-driver/2.5/getting_started/quick_tour/ 安装部分很简单,nuget搜 ...

  8. Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器

    Writing Surface Shaders Writing shaders that interact with lighting is complex. There are different ...

  9. Pytest权威教程(官方教程翻译)

    Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...

随机推荐

  1. MySQL和Python交互

    与Python交互 python3模块名:pymysql conda install pymysql conda install sqlalchemy python2模块名:MySQLdb impor ...

  2. UML学习总结(3)——StarUML指导手册

    StarUML使用说明-指导手册 原著:Stephen Wong            翻译:火猴 StarUML是一种生成类图和其他类型的统一建模语言(UML)图表的工具.这是一个用Java语言描述 ...

  3. [Angular] Stagger animation v4.3.3

    For example, when we open a form, we want to see all the inputs fields comes into one by one. Code f ...

  4. TCP快速重传与快速恢复原理分析(四种不同的算法)

    在TCP/IP中,快速重传和恢复(fast retransmit and recovery,FRR)是一种拥塞控制算法,它能快速恢复丢失的数据包.没有FRR,如果数据包丢失了,TCP将会使用定时器来要 ...

  5. WCF学习笔记——WCF基础

    一 WCF与SOA SOA是一种通过为所有软件提供服务外观,并将这些服务的WSDL集中发布到一个地方的一种组织企业软件的方法.它通过使用明确定义的接口通过跨越边界传递消息来让多个自治的服务协同工作.S ...

  6. Android 蓝牙扫描代码

    /** * Created by rbq on 2016/11/1. */ import android.bluetooth.BluetoothAdapter; import android.blue ...

  7. LA 3602 - DNA Consensus String 枚举

    原题地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. C语言编程程序的内存怎样布局

    在c语言中,每一个变量和函数有两个属性:数据类型和数据的存储类别. C语言中局部变量和全局变量变量的存储类别(static,extern,auto,register) 1. 从变量的作用域划分变量(即 ...

  10. c#编程:给定一个正整数求出是几位数并逆序输出

    <span style="color:#FF0000;">第一步:把输入的数字转为字符串n.ToString() 第二步:求出字符串的长度即为正整数的位数 第三步:从后 ...