我在自定义布局上放置了一个 TextView 以用于通知。 TextView 的颜色自动变为白色。 这是 Android 的错误吗?因为在 Lollipop 之前,通知背景曾经是深色,所以白色文本才有意义。现在在 Lollipop 上,默认背景是白色的,所以白色文本是不可见的。
或者我应该明确地设置文本颜色吗? 放置 android:textColor="@android:color/primary_text_light"使文本变黑,但不能保证所有 Lollipop 设备都有白色通知背景,是吗?
无论系统背景颜色如何,保证自定义通知上的文本始终可见的正确方法是什么?
在 Android Studio 上预览
在实际的 5.1.1 设备上
<?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>
最佳答案
对于仍在寻找这个问题的解决方案的每个人,我认为最好的选择是将 TextView
的样式定义为
TextAppearance.StatusBar.EventContent
在 Lollipop 之前,和
@android:style/TextAppearance.Material.Notification.Title
API >=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"/>
关于android - 5.1.1 通知TextView,处理白色文本的正确方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602738/