gpt4 book ai didi

android - 自定义通知布局和文本颜色

转载 作者:IT老高 更新时间:2023-10-28 13:10:02 25 4
gpt4 key购买 nike

我的应用程序显示了一些通知,并且根据用户偏好,它可能会在通知中使用自定义布局。它运行良好,但有一个小问题 - 文本颜色。普通的 Android 和几乎所有制造商的皮肤都使用黑色文本和浅色背景作为通知文本,但三星没有:他们的通知下拉菜单有深色背景,默认通知布局中的文本是白色的。

所以这会导致一个问题:不使用任何精美布局的通知显示得很好,但使用自定义布局的通知很难阅读,因为文本是黑色而不是默认的白色。甚至official documentation只是为 TextView 设置了一个 #000 颜色,所以我在那里找不到任何指针。

一位用户非常友好地截取了问题的屏幕截图:

Screenshot

那么如何在我的布局中使用来自设备的默认通知文本颜色?我宁愿不开始根据手机型号动态更改文本颜色,因为这需要大量更新,而且使用自定义 ROM 的人可能仍然会遇到问题,具体取决于他们使用的皮肤。

最佳答案

解决方案是使用内置样式。您需要的样式在 Android 2.3 和 Android 4.x 中称为 TextAppearance.StatusBar.EventContent。在 Android 5.x 中, Material 通知使用其他几种样式:TextAppearance.Material.NotificationTextAppearance.Material.Notification.TitleTextAppearance.Material.Notification。第 2 行。只需为 TextView 设置适当的文本外观,您将获得必要的颜色。

如果您对我是如何得出这个解决方案感兴趣的,这里是我的面包屑轨迹。代码摘自 Android 2.3。

  1. 当您使用 Notification 并使用内置方式设置文本时,以下行将创建布局:

    RemoteViews contentView = new RemoteViews(context.getPackageName(),
    com.android.internal.R.layout.status_bar_latest_event_content);
  2. 上述布局包含以下View,它负责查看通知文本:

    <TextView android:id="@+id/text"
    android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:paddingLeft="4dp"
    />
  3. 所以结论是需要的样式是TextAppearance.StatusBar.EventContent,其定义如下:

    <style name="TextAppearance.StatusBar.EventContent">
    <item name="android:textColor">#ff6b6b6b</item>
    </style>

    您应该在此处注意,此样式实际上并未引用任何内置颜色,因此最安全的方法是应用此样式而不是某些内置颜色。

还有一点:在 Android 2.3(API 级别 9)之前,既没有样式,也没有颜色,只有硬编码的值。如果你碰巧因为某种原因不得不支持这样的旧版本,请参阅 answer by Gaks

关于android - 自定义通知布局和文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4867338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com