5.1.1通知TextView,正确处理白色文本的方法是什么?

4
我在自定义布局上放了一个TextView来显示通知,但是TextView的颜色会自动变为白色。这是安卓的一个bug吗?因为在Lollipop之前,通知背景色通常是深色,所以白色文本是有意义的。而现在在Lollipop上,默认背景是白色,所以白色文本就看不见了。
或者说我应该显式地设置文本颜色吗?加上android:textColor="@android:color/primary_text_light"可以将文本变为黑色,但不能保证所有Lollipop设备的通知背景都是白色,对吗?
那么正确的方法是什么,如何确保自定义通知中的文本始终可见,无论系统的背景颜色是什么?
Android Studio预览: Preview on Android Studio 实际5.1.1设备上: On actual 5.1.1 device

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, world"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"/>
</LinearLayout>


你找到解决方案了吗? - Renan Ferreira
1个回答

0
对于仍在寻找此问题解决方案的所有人,我认为最好的选择是将您的TextView样式定义为:
TextAppearance.StatusBar.EventContent

在Lollipop版本之前,以及

@android:style/TextAppearance.Material.Notification.Title

对于APIs >=21,类似这样的东西

styles.xml

<style name="NotificationTextColor.Title" parent="TextAppearance.StatusBar.EventContent"></style>

v21/styles.xml

<style name="NotificationTextColor.Title" parent="@android:style/TextAppearance.Material.Notification.Title"></style>

layout.xml

<TextView
    android:id="@+id/notification_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/NotificationTextColor.Title"
    tools:text="text"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接