android通知栏样式_Android通知样式
android 通知栏样式
We’ve discussed and implemented basic Notifications in 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教程中介绍的⼀些值得注意的功能。
1. Notification Actions
通知动作
2. Heads Up Notification
抬头通知
3. Big Text Style Notification
⼤⽂字样式通知
4. Big Picture Style Notification
⼤图样式通知
5. Inbox Style Notification
收件箱样式通知
6. Message Style Notification
邮件样式通知
Android notification consists of three major components:
Android通知包含三个主要组件:
1. Small Icon (often our app logo)
⼩图标(通常是我们的应⽤徽标)
2. Title
标题
3. 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为我们提供的⼀些预定义超赞样式来设置通知的样式。 我们将详细介绍以下功能。
1. Android Notification Actions
Android通知动作
2. Heads Up Notifications
抬头通知
3. 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
Pre-Nougat Notification Action Design
⽜轧糖前通知动作设计
An example of Notification Actions in Android N and above is given below.
以下是Android N及更⾼版本中的通知操作⽰例。
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 r
ich 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 l is given below:
下⾯给出了l的代码:
<?xml version="1.0" encoding="utf-8"?>
<straint.ConstraintLayout xmlns:android="schemas.android/apk/res/a
ndroid"
xmlns:app="schemas.android/apk/res-auto"
xmlns:tools="schemas.android/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
android:id="@+id/btnHeadsUp"
android:layout_width="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="HEADS UP NOTIFICATION"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnNotificationActions" />
<Button
android:id="@+id/btnBigTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIG TEXT STYLE"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/btnHeadsUp" />
<Button
textstyleandroid:id="@+id/btnBigPictureStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIG PICTURE STYLE"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/btnBigTextStyle" />
<Button
android:id="@+id/btnInboxStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INBOX STYLE"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/btnBigPictureStyle" />
<Button
android:id="@+id/btnMessageStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MESSAGE STYLE"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/btnInboxStyle" />
</straint.ConstraintLayout>
We’ve added a button for each type of notification that we’ll be discussing.我们为将要讨论的每种通知类型添加了⼀个按钮。
The skeleton code for the MainActivity.java is given below.
MainActivity.java的框架代码如下。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnNotificationActions, btnHeadsUpNotification, btnBigTextStyle, btnBigPictureStyle,            btnInboxStyle, btnMessageStyle;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
clearNotification();
btnNotificationActions = (Button) findViewById(R.id.btnNotificationActions);
btnHeadsUpNotification = (Button) findViewById(R.id.btnHeadsUp);
btnBigTextStyle = (Button) findViewById(R.id.btnBigTextStyle);
btnBigPictureStyle = (Button) findViewById(R.id.btnBigPictureStyle);
btnInboxStyle = (Button) findViewById(R.id.btnInboxStyle);
btnMessageStyle = (Button) findViewById(R.id.btnMessageStyle);
btnNotificationActions.setOnClickListener(this);
btnHeadsUpNotification.setOnClickListener(this);
btnBigTextStyle.setOnClickListener(this);
btnBigPictureStyle.setOnClickListener(this);
btnInboxStyle.setOnClickListener(this);
btnMessageStyle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNotificationActions:
notificationActions();
break;
case R.id.btnHeadsUp:
headsUpNotification();
break;
case R.id.btnBigTextStyle:
bigTextStyleNotification();
break;
case R.id.btnBigPictureStyle:
bigPictureStyleNotification();
break;
case R.id.btnInboxStyle:
inboxStyleNotification();
break;
case R.id.btnMessageStyle:
messageStyleNotification();
break;
}
}
public PendingIntent getLaunchIntent(int notificationId, Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("notificationId", notificationId);
Activity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
private void clearNotification() {
int notificationId = getIntent().getIntExtra("notificationId", 0);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        manager.cancel(notificationId);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。