Adding Action Buttons --1.1.2
来源:互联网 发布:卸载软件找不到程序 编辑:程序博客网 时间:2024/05/16 15:06
Action bar 允许你根据上下文向 action items 里面添加按钮。这些按钮通常以图标或者文字的形式出现。那些不适合在直接在 Action bar中显示或者不是很重要的选项通常放在 Action bar 的 overflow(省略视图)里。
1. Specify the Actions in XML
在 Action bar 中的一些按钮和隐藏在 overflow 中的选项通常被定义在 XML 菜单资源文件中。为了向 Action bar 中添加按钮,你需要在工程的 res/menu/ directory 文件夹下创建新的 XML 文件。
向 <item>
元素中添加任何你想为 Action bar 添加的元素。例如:
<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>
上面声明了:“查询”按钮通常应该出现在 Action bar 上(如果空间允许),但是“设置”按钮一直都在 Action bar 的 overflow 里(默认情况下,我们为 Action bar 添加的每一个按钮都出现在 overflow 里)。
上面用到的icon
和string
都应该提前存放在相应的资源文件下。
注意:当你在为应用程序添加icons
或者其他图片资源文件时,你应该多添加几套图片,以备适配。
如果你的 Android SDK 是 2.1,showAsAction
属性的“android”命名空间是不能用在这个版本上的,因此你必须在 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>
2. Add the Actions to the Action Bar
为了给 Action bar 添加 items,你必须实现 onCreateOptionsMenu()
回调函数,并在函数里过滤菜单资源文件,然后将其添加到Menu
对象。例如:
@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);}
3. Respond to Action Buttons
当用户点击了 Action bar 中的按钮或者任何在 overflow 中的按钮时,系统将会回调 onOptionsItemSelected()
。因此为了监听 Action bar 里按钮的点击事件,你必须实现此函数(onOptionsItemSelected()),并在里面调用 MenuItem
的getItemId()
函数,然后匹配你在资源文件中为每一个 item 声明的 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); }}
4. Add Up Button for Low-level Activities
在你的应用程序中,并不是所有的 Activity 都是应用程序的 “main Activity“,因此你必须为这些非”main Activity“提供一个返回主菜单的方法,”Up button“便应运而生了。
当你的应用程序 Android SDK 版本号在 4.1 及以上时,或者你使用的是支持库里的ActionBarActivity
,你只需要在清单文件里声明当前 Activity 的”父“ Activity,然后在对应的 Activity 里面使 Action bar ”可用“即可。例如:
清单文件声明:
<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>
使Action bar 可用:
@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);}
这样做之后,因为系统已经知道DisplayMessageActivity
的”父 “ Activity 是谁,因此你不需要为 ”Up button“做任何监听事件。
好了,这篇文章到这里就结束了,希望能帮到小伙伴,have a good day~
- Adding Action Buttons --1.1.2
- 04 Adding Action Buttons
- Adding Action Buttons
- Adding the Action Bar - Adding Action Buttons
- 【English】Android -> Training -> Adding the Action Bar -> Adding Action Buttons
- Android官方文档翻译 九 2.2Adding Action Buttons
- Adding the Action Bar --1.1
- Floating Action Buttons
- BHO开发浏览器插件-Adding Toolbar Buttons
- 2.15 Adding Buttons to Navigation Bars Using UIBarButtonItem
- 2.21 Adding Buttons to the User Interface with UIButton
- Adding an Easy Share Action
- Buttons
- <<iText in Action 2nd>>4.4 (Adding a table at an absolute position)读书笔记
- 【Android文档】Training-------Adding the Action Bar
- AndroidTraining学习------Adding-the-Action-Bar
- 《Training:Adding an Easy Share Action》
- Adding the Action Bar - Setting Up the Action Bar
- Android面试题之谈谈你对Application类的理解
- 九度OJ 题目1056:最大公约数&&题目1438:最小公倍数
- <Python>try里面是raise不出来的
- STL中list的基本用法范例
- BZOJ3759: Hungergame
- Adding Action Buttons --1.1.2
- First Day at this Blog
- 全排列
- Ubuntu 14.04下为Virtualbox 4.3.20开启usb设备支持
- Playframework中文小站
- Java Collection
- 容器的emplace成员函数
- 技术团队负责人应该具备怎样的能力
- ArrayAdapter和SimpleAdapter详解