ToolBar介绍及使用

来源:互联网 发布:淘宝网登陆界面异常 编辑:程序博客网 时间:2024/06/06 15:02

ToolBar介绍


APP Bar1,应用栏或者操作栏,使用应用栏可让您的应用与其他 Android 应用保持一致,允许用户快速了解如何使用您的应用并获得一流的体验。应用栏的主要功能包括:

  • 一个专用区域,可以标识您的应用并指示用户在应用中的位置;
  • 以可预测的方式访问搜索等重要操作;
  • 支持导航和视图切换(通过标签页或下拉列表);

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library’s version of Toolbar, and they are available on any device that can use the support library.

For this reason, you should use the support library’s Toolbar class to implement your activities’ app bars. Using the support library’s toolbar helps ensure that your app will have consistent behavior across the widest range of devices. For example, the Toolbar widget provides a material design experience on devices running Android 2.1 (API level 7) or later, but the native action bar doesn’t support material design unless the device is running Android 5.0 (API level 21) or later.

从 Android 3.0(API 级别 11)开始,所有使用默认主题的 Activity 均使用 ActionBar 作为应用栏。不过,经过不同 Android 版本的演化,应用栏功能已逐渐添加到原生 ActionBar 中。因此,原生 ActionBar 的行为会随设备使用的 Android 系统的版本而发生变化。相比之下,最新功能已添加到支持库版本的 Toolbar 中,并且这些功能可以在任何能够使用该支持库的设备上使用。

因此,您应使用支持库的 Toolbar 类来实现 Activity 的应用栏。使用支持库的工具栏有助于确保您的应用在最大范围的设备上保持一致的行为。例如,Toolbar 小部件能够在运行 Android 2.1(API 级别 7)或更高版本的设备上提供 Material Design 体验,但除非设备运行的是 Android 5.0(API 级别 21)或更高版本,否则原生操作栏不会支持 Material Design。

所以,官方建议使用支持库的Toolbar来实现所有Activity的运用栏。

向 Activity 添加工具栏

以下步骤说明了如何将 Toolbar 设置为 Activity 的应用栏:

1.引入 v7 appcompat 支持库;

2.使Activity继承 AppCompatActivity,如;

public class MyActivity extends AppCompatActivity {    // ...}

3.添加appcompat 的其中一个 NoActionBar 主题

在应用清单中,将 <application> 元素设置为使用 appcompat 的其中一个 NoActionBar 主题,或者针对某个Activity单独设置主题。使用这些主题中的一个可以防止应用使用原生 ActionBar 类提供应用栏。例如:

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

或者为某个Activity设置主题:

<activity    android:name="...MyActivity"    ...    android:theme="@style/AppTheme.NoActionBar">    ...</activity>

4.在 Activity 的布局添加一个 Toolbar:

<android.support.v7.widget.Toolbar   android:id="@+id/my_toolbar"   android:layout_width="match_parent"   android:layout_height="?attr/actionBarSize"   android:background="?attr/colorPrimary"/>

5.在Activity中引用Toolbar:

在 Activity 的 onCreate() 方法中,调用 Activity 的 setSupportActionBar() 方法,然后传递 Activity 的工具栏。该方法会将工具栏设置为 Activity 的应用栏。例如:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_my);    Toolbar myToolbar = (Toolbar)  findViewById(R.id.my_toolbar);    setSupportActionBar(myToolbar);}

添加和处理工具栏操作

应用栏空间很有限,如果一个应用程序定义了过多按钮,应用栏将多余的按钮放在溢出菜单。运用栏还可以指定一个按钮应该始终显示在溢出菜单还是显示在应用栏中。

添加操作按钮

运用栏上所有的按钮都是定义在 res/menu/ 目录下的 menu resource文件中,所以,添加操作按钮即是添加一个menu文件,并在Activity中onCreateOptionsMenu 指定menu文件。

定义运用栏按钮:

<menu xmlns:android="http://schemas.android.com/apk/res/android">    <!-- should appear as action button if possible -->    <item        android:id="@+id/action_favorite"        android:icon="@drawable/ic_favorite_black_48dp"        android:title="@string/action_favorite"        app:showAsAction="ifRoom"/>    <!--should always be in the overflow -->    <item android:id="@+id/action_settings"          android:title="@string/action_settings"          app:showAsAction="never"/></menu>

在Activity中onCreateOptionsMenu 指定menu文件:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu    getMenuInflater().inflate(R.menu.main, menu);    return true;}

响应操作事件

当用户选择一个应用栏目时,系统调用Activity中的onoptionsitemselected()回调方法,并通过一个MenuItem对象表示这项被点击。

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    switch (item.getItemId())     {        case R.id.action_settings:           ...           return true;        case R.id.action_favorite:           ...           return true;        default:            // Invoke the superclass to handle it.            return super.onOptionsItemSelected(item);    }}

参考


  1. APPBar:https://developer.android.com/training/appbar/setting-up.html ↩
0 0
原创粉丝点击