自定义通知布局和文本颜色 [英] Custom notification layouts and text colors
问题描述
我的应用程序会显示一些通知,并且根据用户偏好,它可能会在通知中使用自定义布局.它运行良好,但有一个小问题 -- 文本颜色.Stock 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.
所以这会导致一个问题:不使用任何花哨布局的通知显示正常,但使用自定义布局的通知很难阅读,因为文本是黑色而不是默认的白色.甚至 官方文档 也只是设置了一个 #
颜色,所以我在那里找不到任何指针.TextView
的 000
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(...)
创建的默认 contentView
.当您找到合适的 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()
.
这是提取默认文本颜色和文本大小(以缩放密度像素为单位 - 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
即.在您的服务的 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屋!