2011-06-06 70 views
12

我有和this post完全一样的问题。我希望我的自定义通知文本样式匹配默认通知(我只是想添加一些额外的视图)。不幸的是,我不完全理解接受的答案。我想我的意思添加到XML代码,但不知道到底是什么...... enter image description here如何使用默认通知样式?

接受的答案说:” 的解决方案是使用内置样式,你需要的风格是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

回答

24

终于想通了什么,我做错了......(基本上我当我仅在API 8上开发时,使用API​​ 9的功能)。

首先,使用默认(平台)风格使用...

风格= “@android:风格/ 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" /> 

其次使用状态栏的默认样式使用..

风格= “@android:风格/ TextAppearance.StatusBar.EventContent” 或

风格= “@android:风格/ 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)开发,而这些状态栏样式对于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

+0

只是让我们很清楚...有没有办法用API9之前默认的通知样式,是否正确? – 2011-07-01 18:25:14

+0

不幸的是,是的。正如我在我的回答(http://stackoverflow.com/questions/4867338/custom-notification-layouts-and-text-colors/4935191#4935191)中所解释的,在API Level 8及更早版本中,只有一个硬编码值,无法访问。 – Malcolm 2011-07-18 18:57:57

+1

答案(http://stackoverflow.com/a/7320604/435605)确实提供了一种方法来查找2.2- – 2011-12-06 19:36:39