【问题标题】:How to get the notification background color after android Nandroid N后如何获取通知背景颜色
【发布时间】:2018-03-11 03:09:10
【问题描述】:

我想定义通知界面,因为在某些手机中通知背景颜色是白色的,而在其他手机中是深色的。我想检查背景颜色是什么,以便选择不同的布局。

在android N之前,我的代码是这样的:

https://github.com/lizixian18/MusicLibrary/blob/master/musiclibrary/src/main/java/com/lzx/musiclibrary/notification/NotificationColorUtils.java#isDarkNotificationBar

原理是检查默认contentView标题和内容TextView颜色。但是在android N之后,默认的contentViewnull

通知#build:

public Notification build() {

    if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
            && (useExistingRemoteView())) {
        if (mN.contentView == null) {
            mN.contentView = createContentView();
            mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
                    mN.contentView.getSequenceNumber());
        }
        if (mN.bigContentView == null) {
            mN.bigContentView = createBigContentView();
            if (mN.bigContentView != null) {
                mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
                        mN.bigContentView.getSequenceNumber());
            }
        }
        if (mN.headsUpContentView == null) {
            mN.headsUpContentView = createHeadsUpContentView();
            if (mN.headsUpContentView != null) {
                mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
                        mN.headsUpContentView.getSequenceNumber());
            }
        }
    }

    if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
        mN.flags |= FLAG_SHOW_LIGHTS;
    }
    return mN;
}

所以我的问题是:Android N之后如何判断通知背景颜色是深还是白?

【问题讨论】:

  • 您是否只是想获取默认背景颜色以便设置文本颜色?
  • 是的,你是对的

标签: android colors notifications background


【解决方案1】:

建议在通知文本上设置自定义颜色。相反,您应该在文本中使用TextAppearance.Compat.Notification styles

  • TextAppearance.Compat.Notification 通知中的常规文本
  • TextAppearance.Compat.Notification.Title 为标题样式
  • TextAppearance.Compat.Notification.Info(或Line2)用于辅助文本

这些样式自动内置了正确的颜色。

对于您提到的媒体应用程序,通知应使用NotificationCompat.MediaStyle 构建,这还考虑了正确的背景和文本颜色。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 2018-03-09
  • 2014-01-03
  • 2016-10-27
相关资源
最近更新 更多