自定义通知布局和文字颜色 [英] Custom notification layouts and text colors

查看:308
本文介绍了自定义通知布局和文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用表明某些通知,并且根据用户preferences它可能使用自定义布局中的通知。它运作良好,但有一个小问题 - 文字颜色。股票Android和几乎所有的制造商使用的皮肤对一个浅色背景的通知文本黑色文本,但三星并不:他们通知下拉有一个黑暗的背景中默认的通知布局文本为白色

My application shows some notifications, and depending on user preferences it might use a custom layout in a notification. It works well, but there is a small problem -- text colors. Stock Android and almost all manufacturer skins use black text against a light background for notification text, but Samsung doesn't: their notification pulldown has a dark background and the text in the default notification layout is white.

所以,这会导致一个问题:不使用任何花哨布局的通知显示了罚款,但使用自定义布局之一就是难以阅读,因为文字是黑色的,而不是默认的白色。即使是<一个href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification">official文档只是设置一个#000 颜色为的TextView ,所以我找不到任何指针有

So this causes a problem: the notifications that don't use any fancy layouts show up fine, but the one that uses a custom layout is hard to read because the text is black instead of the default white. Even the official documentation just sets a #000 color for a TextView, so I couldn't find any pointers there.

一个用户友好地对待这一问题的截图:

A user was kind enough to take a screenshot of the problem:

所以如何使用默认的通知文本颜色从设备在我的布局?我宁愿不启动动态改变的基础上的手机型号文字的颜色,因为这需要大量的更新和人民自定义ROM的可能仍然得到这个问题,这取决于他们所使用的皮肤。

So how do I use the default notification text color from the device in my layouts? I'd rather not start dynamically altering the text color based on phone model, since that requires a lot of updating and people with custom ROM's might still get the problem, depending on the skin they're using.

推荐答案

由Malcolm解决方案正常工作与API> = 9。下面是较旧的API的解决方案:

Solution by Malcolm works fine with API>=9. Here's the solution for older API:

关键是要建立标准的通知对象,然后遍历默认内容查看 Notification.setLatestEventInfo(...)。当你找到合适的TextView,刚拿到 tv.getTextColors()。getDefaultColor()

The trick is to create the standard notification object and then traverse the default contentView created by Notification.setLatestEventInfo(...). When you find the right TextView, just get the tv.getTextColors().getDefaultColor().

这里的code提取默认的文字颜色和文字大小(缩放像素密度 - SP)。

Here's the code that extracts the default text color and text size (in scaled density pixels - sp).

private Integer notification_text_color = null;
private float notification_text_size = 11;
private final String COLOR_SEARCH_RECURSE_TIP = "SOME_SAMPLE_TEXT";

private boolean recurseGroup(ViewGroup gp)
{
    final int count = gp.getChildCount();
    for (int i = 0; i < count; ++i)
    {
        if (gp.getChildAt(i) instanceof TextView)
        {
            final TextView text = (TextView) gp.getChildAt(i);
            final String szText = text.getText().toString();
            if (COLOR_SEARCH_RECURSE_TIP.equals(szText))
            {
                notification_text_color = text.getTextColors().getDefaultColor();
                notification_text_size = text.getTextSize();
                DisplayMetrics metrics = new DisplayMetrics();
                WindowManager systemWM = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
                systemWM.getDefaultDisplay().getMetrics(metrics);
                notification_text_size /= metrics.scaledDensity;
                return true;
            }
        }
        else if (gp.getChildAt(i) instanceof ViewGroup)
            return recurseGroup((ViewGroup) gp.getChildAt(i));
    }
    return false;
}

private void extractColors()
{
    if (notification_text_color != null)
        return;

    try
    {
        Notification ntf = new Notification();
        ntf.setLatestEventInfo(this, COLOR_SEARCH_RECURSE_TIP, "Utest", null);
        LinearLayout group = new LinearLayout(this);
        ViewGroup event = (ViewGroup) ntf.contentView.apply(this, group);
        recurseGroup(event);
        group.removeAllViews();
    }
    catch (Exception e)
    {
        notification_text_color = android.R.color.black;
    }
}

呼叫 extractColors IE浏览器。中的onCreate()为您服务。然后,当你创建自定义通知,你要的颜色和文字大小是 notification_text_color notification_text_size

Call extractColors ie. in onCreate() of your service. Then when you're creating the custom notification, the color and text size you want are in notification_text_color and notification_text_size:

Notification notification = new Notification();
RemoteViews notification_view = new RemoteViews(getPackageName(), R.layout.notification);       
notification_view.setTextColor(R.id.label, notification_text_color);
notification_view.setFloat(R.id.label, "setTextSize", notification_text_size);

这篇关于自定义通知布局和文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆