自定义通知布局和文本颜色我的应用程序显示了一些通知,并且根据用户首选项,它可能会在通知中使用自定义布局。它运作良好,但有一个小问题 - 文本颜色。股票Android和几乎所有制造商皮肤都使用黑色文本作为通知文本的浅色背景,但三星没有:他们的通知下拉具有深色背景,默认通知布局中的文本是白色。所以这会导致一个问题:不使用任何花哨布局的通知显示正常,但使用自定义布局的通知很难阅读,因为文本是黑色而不是默认白色。即使官方文档只是#000为a 设置颜色TextView,所以我找不到任何指针。那么如何在布局中使用设备中的默认通知文本颜色?我宁愿不开始根据手机型号动态改变文本颜色,因为这需要大量更新,而定制ROM的人可能仍会遇到问题,具体取决于他们使用的皮肤。
3 回答
心有法竹
TA贡献1866条经验 获得超5个赞
Malcolm的解决方案适用于API> = 9。以下是旧API的解决方案:
诀窍是创建标准通知对象,然后遍历contentView创建的默认值Notification.setLatestEventInfo(...)。当你找到合适的TextView时,就得到了tv.getTextColors().getDefaultColor()。
这是提取默认文本颜色和文本大小的代码(以缩放密度像素 - 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:
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);
宝慕林4294392
TA贡献2021条经验 获得超8个赞
以下是仅使用资源的任何SDK版本的解决方案。
RES /值/ styles.xml
<?xml version="1.0" encoding="utf-8"?><resources> <style name="NotificationTitle"> <item name="android:textColor">?android:attr/textColorPrimaryInverse</item> <item name="android:textStyle">bold</item> </style> <style name="NotificationText"> <item name="android:textColor">?android:attr/textColorPrimaryInverse</item> </style></resources>
RES /值-V9 / styles.xml
<?xml version="1.0" encoding="utf-8"?><resources> <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" /></resources>
RES /布局/ my_notification.xml
...<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="title" style="@style/NotificationTitle" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text" style="@style/NotificationText" />...
PS:硬编码值用于2.2-。因此,一些罕见的旧自定义固件可能会出现问题。
- 3 回答
- 0 关注
- 596 浏览
添加回答
举报
0/150
提交
取消
