2011-06-06 75 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