android - 5.1.1 通知TextView,处理白色文本的正确方式?

标签 android android-layout android-5.0-lollipop

我在自定义布局上放置了一个 TextView 以用于通知。 TextView 的颜色自动变为白色。 这是 Android 的错误吗?因为在 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>

最佳答案

对于仍在寻找这个问题的解决方案的每个人,我认为最好的选择是将 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/

相关文章:

android - 如何以编程方式调用 ViewPager 的下一页?

android - android :layout_alignParentTop and android:layout_alignParentStart之间的区别

android - 找不到与给定名称匹配的资源 'android:Theme.Material.Light.DarkActionBar'

java - 获取ArrayAdapter的 Activity 项

android - 如何构建两个具有不同 API 端点的 Android 应用程序版本而无需每次都编辑代码?

android - 多个 Activity 的 ViewPager

android - 底部工具栏菜单项提示出现在顶部

android - 带有 AppCompat-v7 的工具栏和上下文操作栏

android - 解析数据时出错 - 字符串无法转换为 JSONObject

Java Android Basic 游戏 - 使用共享首选项保存高分 - 初学者