82

私のアプリケーションはいくつかの通知を表示します。ユーザー設定によっては、通知でカスタム レイアウトを使用する場合があります。うまく機能しますが、小さな問題があります -テキストの色。標準の Android とほとんどすべてのメーカーのスキンは、通知テキストに明るい背景に対して黒のテキストを使用しますが、Samsung はそうではありません。通知プルダウンの背景は暗く、デフォルトの通知レイアウトのテキストは白です。

したがって、これは問題を引き起こします。派手なレイアウトを使用しない通知は問題なく表示されますが、カスタム レイアウトを使用する通知は、テキストがデフォルトの白ではなく黒であるため、読みにくくなります。公式ドキュメントでさえ の#000色を設定するだけなTextViewので、そこにポインタが見つかりませんでした。

ユーザーは親切にも問題のスクリーンショットを撮ってくれました:

スクリーンショット

では、デバイスのデフォルトの通知テキストの色をレイアウトで使用するにはどうすればよいでしょうか? 電話のモデルに基づいてテキストの色を動的に変更することは避けたいと思います。これには多くの更新が必要であり、カスタム ROM を使用している人は、使用しているスキンによっては問題が発生する可能性があるためです。

4

12 に答える 12

87

解決策は、組み込みのスタイルを使用することです。必要なスタイルはTextAppearance.StatusBar.EventContent、Android 2.3 および Android 4.x で呼び出されます。Android 5.x のマテリアル通知ではTextAppearance.Material.Notification、 、TextAppearance.Material.Notification.Title、およびTextAppearance.Material.Notification.Line2. テキスト ビューに適切なテキストの外観を設定するだけで、必要な色が得られます。

私がどのようにしてこの解決策にたどり着いたかに興味がある場合は、ここに私のパンくずリストがあります。コードの抜粋は Android 2.3 のものです。

  1. 組み込みの手段を使用してテキストを使用Notificationおよび設定すると、次の行でレイアウトが作成されます。

    RemoteViews contentView = new RemoteViews(context.getPackageName(),
            com.android.internal.R.layout.status_bar_latest_event_content);
    
  2. 上記のレイアウトには、View通知テキストの表示を担当する次のものが含まれています。

    <TextView android:id="@+id/text"
        android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:paddingLeft="4dp"
        />
    
  3. したがって、必要なスタイルはTextAppearance.StatusBar.EventContentであり、その定義は次のようになります。

    <style name="TextAppearance.StatusBar.EventContent">
        <item name="android:textColor">#ff6b6b6b</item>
    </style>
    

    ここで、このスタイルは実際には組み込みの色を参照していないことに注意してください。したがって、最も安全な方法は、組み込みの色の代わりにこのスタイルを適用することです。

もう 1 つ: Android 2.3 (API レベル 9) より前は、スタイルも色もなく、ハードコードされた値しかありませんでした。何らかの理由でそのような古いバージョンをサポートする必要がある場合は、Gaks による回答を参照してください。

于 2011-02-08T15:50:46.310 に答える
63

Malcolm によるソリューションは、API>=9 で正常に動作します。古い API の解決策は次のとおりです。

秘訣は、標準の通知オブジェクトを作成してから、contentViewによって作成されたデフォルトをトラバースすることですNotification.setLatestEventInfo(...)。適切な TextView が見つかったら、tv.getTextColors().getDefaultColor().

デフォルトのテキストの色とテキストのサイズ (スケーリングされた密度ピクセル - sp) を抽出するコードを次に示します。

private Integer notification_text_color = null;
private float notification_text_size = 11;
private final String COLOR_SEARCH_RECURSE_TIP = "SOME_SAMPLE_TEXT";

private boolean recurseGroup(ViewGroup gp)
{
    final int count = gp.getChildCount();
    for (int i = 0; i < count; ++i)
    {
        if (gp.getChildAt(i) instanceof TextView)
        {
            final TextView text = (TextView) gp.getChildAt(i);
            final String szText = text.getText().toString();
            if (COLOR_SEARCH_RECURSE_TIP.equals(szText))
            {
                notification_text_color = text.getTextColors().getDefaultColor();
                notification_text_size = text.getTextSize();
                DisplayMetrics metrics = new DisplayMetrics();
                WindowManager systemWM = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
                systemWM.getDefaultDisplay().getMetrics(metrics);
                notification_text_size /= metrics.scaledDensity;
                return true;
            }
        }
        else if (gp.getChildAt(i) instanceof ViewGroup)
            return recurseGroup((ViewGroup) gp.getChildAt(i));
    }
    return false;
}

private void extractColors()
{
    if (notification_text_color != null)
        return;

    try
    {
        Notification ntf = new Notification();
        ntf.setLatestEventInfo(this, COLOR_SEARCH_RECURSE_TIP, "Utest", null);
        LinearLayout group = new LinearLayout(this);
        ViewGroup event = (ViewGroup) ntf.contentView.apply(this, group);
        recurseGroup(event);
        group.removeAllViews();
    }
    catch (Exception e)
    {
        notification_text_color = android.R.color.black;
    }
}

extractColorsつまり、呼び出します。サービスの onCreate() で。次に、カスタム通知を作成するときに、必要な色とテキスト サイズが次のようになりnotification_text_colorますnotification_text_size

Notification notification = new Notification();
RemoteViews notification_view = new RemoteViews(getPackageName(), R.layout.notification);       
notification_view.setTextColor(R.id.label, notification_text_color);
notification_view.setFloat(R.id.label, "setTextSize", notification_text_size);
于 2011-09-06T13:21:42.060 に答える
17

リソースのみを使用する任意の SDK バージョンのソリューションを次に示します。

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationTitle">
      <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
      <item name="android:textStyle">bold</item>
    </style>
    <style name="NotificationText">
      <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
    </style>
</resources>

res/values-v9/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
</resources>

res/layout/my_notification.xml

...
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="title"
    style="@style/NotificationTitle"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="text"
    style="@style/NotificationText"
    />
...

PS: 2.2 以降では、ハードコードされた値が使用されます。そのため、まれに古いカスタム ファームウェアで問題が発生する場合があります。

于 2012-01-06T04:58:55.437 に答える
13

2.3以降の場合(Androidのドキュメントから):

android:TextAppearance.StatusBar.EventContent.Titleプライマリテキストのスタイルを使用します。

android:TextAppearance.StatusBar.EventContent二次テキストのスタイルを使用します。

2.2-の場合、このスレッドに対する別の回答でGaksが提案したことを実行します。

2.2に対してコンパイルし、2.3以降をサポートし、さまざまなデバイスをすべてサポートしたい場合、私が知っているのはGaksのソリューションだけです。

ところで、Googleが?android:attr/textColorPrimary2.2-の値を使用することについて提案したことは機能していません。エミュレータを使用して試してみてください。Gaksの方法が唯一の方法です。

その他のリソース:これこれは機能しません。

于 2011-12-06T19:21:39.507 に答える
2

で指定された色を使用する必要があります。android.R.color

例えば:android.R.color.primary_text_light

カスタム ROM 開発者と Android スキン デザイナーはこれらを更新して、アプリの色がシステムの他の部分と一致するようにする必要があります。これには、テキストがシステム全体で適切に表示されるようにすることも含まれます。

于 2011-02-02T02:40:27.460 に答える
1

Androidが提供する属性の名前を直接変更する非常に簡単な解決策を見つけました。

このチュートリアルでわかるように:http: //www.framentos.com/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/

別の属性を使用するだけで済みます。

<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>

これがお役に立てば幸いです。

于 2012-02-21T18:49:59.217 に答える
1

私はそれが古い質問であることを知っていますが、他の誰かを助けることができます; )私は自分のアプリでこれを行い、数行で完璧に機能します:

    RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);

    if (SDK >= LOLLIPOP) {

            TextView textView = new TextView(context);
            textView.setTextAppearance(context, android.R.style.TextAppearance_Material_Notification_Title);

            notificationView.setTextColor(R.id.title, textView.getCurrentTextColor());
            notificationView.setFloat(R.id.title, "setTextSize", textView.getTextSize());

            textView.setTextAppearance(context,android.R.style.TextAppearance_Material_Notification_Line2);

            notificationView.setTextColor(R.id.contentText,textView.getCurrentTextColor());
            notificationView.setFloat(R.id.contentText,"setTextSize",textView.getTextSize());

            textView = null;

    }
于 2017-01-01T22:38:32.077 に答える
1

この手順を見てください: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView LinearLayout コンテナの背景色を設定すると、テキストと通知の色を通知に含めることができます。背景。

通知テキストのデフォルトの色がランチャー アプリケーションによって定義されている場合、ランチャーがこの情報を共有していない限り、Android のデフォルト設定から取得することはできません。

ただし、この行 android:textColor="#000" をレイアウトから削除して、自動的にデフォルトの色を取得できるようにしましたか?

于 2011-02-06T05:34:04.363 に答える
0

このエラーを解決するために私が働いたのは次のとおりです。以下を styles.xml に追加します。

    <style name="TextAppearance">
    </style>
    <style name="TextAppearance.StatusBar">
    </style>
    <style name="TextAppearance.StatusBar.EventContent">
        <item name="android:textColor">#ff6b6b6b</item>
    </style>
    <style name="TextAppearance.StatusBar.EventContent.Info">
        <item name="android:textColor">#ff6b6b6b</item>
    </style>
于 2019-05-13T18:48:11.917 に答える