【问题标题】:How to use default notification style?如何使用默认通知样式?
【发布时间】:2023-03-08 12:46:01
【问题描述】:

我的问题与this post 完全相同。我希望我的自定义通知文本样式与默认通知匹配(我只是要添加一些额外的视图)。不幸的是,我不完全理解接受的答案。我想我打算添加到 XML 代码中,但不确定到底是什么......

接受的答案说“ 解决方案是使用内置样式。您需要的样式是 TextAppearance.StatusBar.EventContent。只需应用此样式,它将为通知设置默认文本颜色(当然,不要忘记 android: 前缀)。 "

我无法让它工作!在我的自定义通知中,“android:textAppearance="?android:attr/textAppearanceLarge" 行下方有效(因为它放大了文本)但没有达到预期的效果。

这是我的自定义 XML 代码...

<ImageView
    android:id="@+id/notImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp" 
    android:layout_alignParentTop="true"/>

<TextView 
    android:id="@+id/notContentTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf ="@id/notImage" 
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView 
    android:id="@+id/notContentText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below ="@id/notContentTitle"
 />

Custom notification layouts and text colors

【问题讨论】:

    标签: android notifications styles


    【解决方案1】:

    终于弄清楚我做错了什么......(当我只在 API 8 上开发时,基本上我使用的是 API 9 的功能)。

    首先,使用默认(平台)样式使用...

    style = "@android:style/TextAppearance.Small"

    例如...

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="3dp">
    
    <ImageView
        android:id="@+id/notImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" 
        android:layout_alignParentTop="true"/>
    
    <TextView 
        android:id="@+id/notContentTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf ="@id/notImage" 
        android:textStyle="bold"
        style = "@android:style/TextAppearance.Medium"/>
    
    <TextView 
        android:id="@+id/notContentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below ="@id/notImage"
        style = "@android:style/TextAppearance.Small"    />
    

    其次要使用StatusBar的默认样式使用..

    style = "@android:style/TextAppearance.StatusBar.EventContent" 或

    style = "@android:style/TextAppearance.StatusBar.EventContent.Title",

    等等等等

    例如,

        <TextView 
        android:id="@+id/notContentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below ="@id/notImage"
        style = "@android:style/TextAppearance.StatusBar.EventContent"   />
    

    就我而言,这导致了一个错误,因为我仍在使用 Android 2.2 (API 8) 进行开发,而这些 StatusBar 样式适用于 API 9 及更高版本。 (我知道我应该更新:))

    有用的链接是;

    Android.com Applying Styles and Themes, Using Platform Styles and Themes

    Android.com R.style reference

    StackOverflow.com Custom notification layouts and text colours

    Android.com Android API levels

    【讨论】:

    • 我们很清楚...没有办法使用 API9 之前的默认通知样式,对吗?
    • 不幸的是,是的。正如我在回答 (stackoverflow.com/questions/4867338/…) 中所解释的那样,在 API 级别 8 及更早版本中,只有一个无法访问的硬编码值。
    • 答案 (stackoverflow.com/a/7320604/435605) 确实提供了一种查找 2.2 的颜色的方法-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2020-03-31
    相关资源
    最近更新 更多