博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android官方教程翻译(6)——添加ActionBar
阅读量:6242 次
发布时间:2019-06-22

本文共 5556 字,大约阅读时间需要 18 分钟。

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 . 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

添加方法如下:

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 .

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命名空间应该是你应用的名字,但是这个名字是可以任意取的,最好是有意义并可理解的名称 )如下:

...

Add the Actions to the Action Bar

给ActionBar添加Action

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

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

@Overridepublic 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  callback method. In your implementation of this method, call on the given  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.

@Overridepublic 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 using 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中如何声明:

...
...
Then enable the app icon as the 
Up
 button by calling 
:

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

@Overridepublic 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 the
Up
 button's event.

转载于:https://www.cnblogs.com/lanzhi/p/6469511.html

你可能感兴趣的文章
[转] vue自定义组件(通过Vue.use()来使用)即install的使用
查看>>
[转] 函数声明和函数表达式——函数声明的声明提前
查看>>
敢死队2影评
查看>>
浅析 JavaScript 中的 apply 和 call 用法的差异
查看>>
html5-css综合练习
查看>>
嵌入式开发之cgic库---cgi库的使用
查看>>
clickhouse安装 Requires: libstdc++.so.6(GLIBCXX_3.4.19)(64bit)
查看>>
FFT快速傅立叶变换
查看>>
<刘未鹏 MIND HACKS>读书笔记
查看>>
locate
查看>>
AceyOffice教程--如何判断单元格的内容
查看>>
前端 -- 超链接导航栏案例
查看>>
软工网络15个人作业
查看>>
css 兼容性写法,CSS hack写法
查看>>
剑指offer 之 C/C++基础知识1
查看>>
(KMP 暴力)Corporate Identity -- hdu -- 2328
查看>>
Silverlight程序中访问配置文件
查看>>
Linux下利用rsync实现多服务器文件同步
查看>>
2.3 Rust函数
查看>>
1.3 IDAE 中使用GO开发项目
查看>>