5.1.1 notification TextView, the correct way to deal with white text?

狂风中的少年 提交于 2019-12-10 17:06:57

问题


I put a TextView on a custom layout for a notification. The colour of the TextView becomes automatically white. Is this a bug of Android? Because prior to Lollipop, notification background used to be a dark colour, so a white text made sense. Now on Lollipop, the default background is white, so the white text is invisible.

Or am I supposed to set text colour explicitely? Putting android:textColor="@android:color/primary_text_light" made the text black, but it is not guaranteed that all Lollipop devices have white notification background, is it?

What is the correct way to guarantee that the text on a custom notification is always visible regardless of the background colour of the system?

Preview on Android Studio

On an 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>

回答1:


For everyone still looking for a solution for this question, I think that the best alternative is to define the style of your TextView to

TextAppearance.StatusBar.EventContent

prior to lollipop, and

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

for APIs >=21. Something like this

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"/>


来源:https://stackoverflow.com/questions/30602738/5-1-1-notification-textview-the-correct-way-to-deal-with-white-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!