android 通知栏样式
We’ve discussed and implemented basic Notifications in this post. In this tutorial, we’ll be looking into more advanced features of android notification and styling our notifications in different ways.
我们已经讨论并实现基本声明此文章。 在本教程中,我们将研究android通知的更高级功能,并以不同方式设置通知样式。
Android通知样式 (Android Notification Styling)
Android Notification have plenty of powerful features that when used correctly would significantly improve the UX and user engagement. Some noteworthy features that we’ll be covering in this Android Notification tutorial are listed below.
Android Notification具有许多强大的功能,如果正确使用它们,将大大提高用户体验和用户参与度。 下面列出了我们将在本Android Notification教程中介绍的一些值得注意的功能。
- Notification Actions 通知动作
- Heads Up Notification 抬头通知
- Big Text Style Notification 大文字样式通知
- Big Picture Style Notification 大图样式通知
- Inbox Style Notification 收件箱样式通知
- Message Style Notification 邮件样式通知
Android notification consists of three major components:
Android通知包含三个主要组件:
- Small Icon (often our app logo) 小图标(通常是我们的应用徽标)
- Title 标题
- Content Text 内容文字
The introduction of Android Nougat (API 24) has given a new look to notifications as shown below.
Android Nougat(API 24)的引入使通知有了新外观,如下所示。
The large icon now appears on the right. There’s an arrow besides the notification title that lets us expand, collapse the notification.
现在,大图标出现在右侧。 通知标题旁边有一个箭头,可让我们展开,折叠通知。
In this tutorial, we’ll be styling our notifications using some pre-defined awesome styles that Android has in store for us. We’ll be covering the following features at length.
在本教程中,我们将使用Android为我们提供的一些预定义超赞样式来设置通知的样式。 我们将详细介绍以下功能。
- Android Notification Actions Android通知动作
- Heads Up Notifications 抬头通知
- Rich Notifications 丰富的通知
Android通知动作 (Android Notification Actions)
Android Notification actions are basically buttons placed below the body of the notification. A Notification action must include an icon, a label, and a PendingIntent
to be fired when the action is selected by the user.
Android Notification动作基本上是位于通知主体下方的按钮。 通知动作必须包含一个图标,一个标签以及一个当用户选择该动作时要触发的PendingIntent
。
With the Introduction of Android N, the icons are omitted from the action buttons to give space to other components.
随着Android N的介绍,操作按钮中的图标被省略,以留出空间给其他组件。
An example of notification actions in Pre Nougat devices is given below.
牛轧糖前设备中的通知操作示例如下。
![android notification before android N](https://cdn.journaldev.com/wp-content/uploads/2017/09/android-notification-actions-pre-nougat-450x142.png)
Pre-Nougat Notification Action Design
牛轧糖前通知动作设计
An example of Notification Actions in Android N and above is given below.
以下是Android N及更高版本中的通知操作示例。
![android notification tutorial](https://i-blog.csdnimg.cn/blog_migrate/97ae3e1a4971cd2dfcbbf54443a9b1dd.png)
Notification Action in Android Nougat
Android Nougat中的通知动作
抬头通知 (Heads Up Notifications)
With the introduction of Android Lollipop (API level 21), notifications can appear in a small floating window (also called a heads-up notification) when the device is active (that is, the device is unlocked and its screen is on).
随着Android Lollipop(API级别21)的引入,当设备处于活动状态(即设备已解锁且屏幕处于打开状态)时,通知可以显示在一个小的浮动窗口中(也称为平视通知)。
Such type of notifications are commonly seen when you’re using an application and you get a call. A small floating notification known as heads up notifications pops up with the notification actions to accept or decline a call.
当您使用应用程序并接到电话时,通常会看到此类通知。 一个小的浮动通知(称为抬头通知)随通知动作弹出,以接听或拒绝呼叫。
丰富的通知 (Rich Notifications)
Android allows us to add rich styles to our application with the introduction of Android L. Using these styles would make the notification look more appealing than ever. Some of the known styles that are used in many applications are listed below and are self-explanatory.
Android允许我们通过引入Android L向我们的应用程序添加丰富的样式。使用这些样式将使通知看起来比以往更具吸引力。 下面列出了许多应用程序中使用的一些已知样式,这些样式是不言自明的。
- BigTextStyle BigTextStyle
- BigPictureStyle BigPictureStyle
- InboxStyle InboxStyle
- MessageStyle MessageStyle
We know that notifications on Android N can be expanded using the arrows. To expand notifications on pre-Nougat versions, you can swipe down over the notification using two fingers.
我们知道可以使用箭头扩展Android N上的通知。 要扩展牛轧糖之前版本的通知,您可以用两根手指在通知上向下滑动。
Not all Android versions would support the above styles. In case an Android OS doesn’t support the rich style, that style would simply be ignored.
并非所有的Android版本都支持上述样式。 如果Android操作系统不支持丰富样式,则该样式将被忽略。
Now let’s jump into the business end of this tutorial and develop an application that’ll have all the above features.
现在,让我们进入本教程的业务范围,并开发一个具有上述所有功能的应用程序。
Android Notification Tutorial项目结构 (Android Notification Tutorial Project Structure)
Android通知教程代码 (Android Notification Tutorial Code)
The code for the activity_main.xml
is given below:
下面给出了activity_main.xml
的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.journaldev.stylingnotifications.MainActivity">
<Button
android:id="@+id/btnNotificationActions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NOTIFICATION ACTIONS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.196" />
<Button
andr