Adding The ActionBar(添加ActionBar)

来源:互联网 发布:数据库日常维护 编辑:程序博客网 时间:2024/05/19 16:49

ActionBar

1.设置ActionBar

Android3.0和以上系统

从Android 3.0 API 11开始,action bar已经被包含在所有使用Theme.Holo(或子类) 主题的activity里边了,也就是默认的主题,当你把目标sdk版本和最小sdk版本设为11以上时.

支持Android 2.1及以上

要在Android 3.0以下2.1以上运行action bar需要包含 安卓支持库到程序里.添加v7 appcompat 库

1.升级 activity 继承自 ActionBarActivity.

public class MainActivity extends ActionBarActivity { ... }

2.配置manifest文件更新程序的主题为 Theme.AppCompat.

<activity android:theme="@style/Theme.AppCompat.Light" ... ><manifest ... >    <uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18" />    ...</manifest>

2.添加行为按钮

action bar 允许为应用当前的上下文涉及道德重要行为添加按钮.直接在action bar上显示一个icon 或者 文本 作为一个行为按钮.不重要的行为不能够放在action bar上而是隐藏在行为菜单里.

在XML文件中指定行为

所有的行为按钮和其他的隐藏按钮被定义在 menu resource XML(菜单资源)文件中.要添加行为到action bar,创建一个XML文件在项目的 res/menu/目录下.要想添加在 action bar中添加条目使用 <item> 标签元素

res/menu/main_activity_actions.xml

<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空间足够就会显示出来,但是设置行为只会出现在下拉菜单里.(默认的所有行为出现在下拉菜单里,但是这样写能够明确定义每个行为的意图.)

如果使用了 支持库 兼容 2.1版本,那么 showAsAction 属性就不能直接使用 android: 命名空间,支持库提供了用来替换的这个属性你必须定义自己的命名空间并且使用命名空间作为属性前缀.

res/menu/main_activity_actions.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>

把 行为 添加到 Action Bar

要把菜单项目放到action bar中,需要在你的activity中实现 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);}

响应 行为按钮

当用户按下行为按钮或者菜单中的行为,系统会调用你的activity中的 onOptionsItemSelected() 回调方法.在你的方法实现中通过 调用 MenuItem的getItemId() 方法来判断是哪个按钮被按下.返回的id对应于你在元素标签中定义的 android:id 属性.

给后启动的Activity添加返回按钮(Add Up Button for Low-level Activities)

程序中的非首页屏幕应该在Action Bar上给用户提供一个导航到逻辑父屏幕的按钮

如果运行在 Android 4.1(API 16)或者更高,或使用了支持库的 ActionBarActivity,要执行向上导航只需要在manifest文件中简单的定义父 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>

因为系统现在知道了 MainActivity 是DisplayMessageActivity的父activity,当用户点击了 向上按钮,系统会适当的导航到父activity–你不需要控制 向上按钮 的事件.

设计Action Bar 风格

Action Bar 提供给用户一个他所熟悉的方式来执行动作和在你APP内的导航,但并不意味着它在其他程序里看起来也是合适的.如果你想要修改action bar的样式来适应你的产品,可以很简单的使用 主题和样式资源来进行配置。

Android 包含了一些 内置的 activity 主题 有 “dark” 或者 “light” action bar 样式.你可以继承这些主题来进一步的自定义你的action bar外观.

Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform’s style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app). See the examples below for details.

使用一个安卓主题

安卓包含两个基本的activity 主题指示了ActionBar的颜色:
- Theme.Holo for a “dark” theme.
- Theme.Holo.Light for a “light” theme.

你可以在manifest文件中定义 android:theme 属性 给元素来应用这些主题给整个APP或者单独给元素定义这个属性来单独给一个activity设置

<application android:theme="@android:style/Theme.Holo.Light" ... />

即使使用一个亮色的主题也能定义一个暗色的ActionBar,通过定义主题为:Theme.Holo.Light.DarkActionBar

当使用 支持库的时候,必须替换为 Theme.AppCompat 主题:

  • Theme.AppCompat for the “dark” theme.
  • Theme.AppCompat.Light for the “light” theme.
  • Theme.AppCompat.Light.DarkActionBar for the light theme with a dark action bar.

确保你使用的ActionBar 图标与ActionBar形成适当的对比。

自定义背景
要改变ActionBar背景,要给你的activity创建一个自定义的主题需要覆盖actionBarStyle 属性.这个属性指向了另一个样式用来覆盖背景属性,通过指定一个drawable资源给actionBar设置背景.

0 0
原创粉丝点击