Android,自定义通知,给我总是白色文字

时间:2015-04-27 19:34:00

标签: android notifications push-notification android-notifications

我正在尝试制作一个自定义通知栏,它可以正常工作,但是当我在棒棒糖中测试它时,它会显示带有白色文本的标题和内容文本,这对于kitkat很有用,因为通知栏很暗但不是棒棒糖。

如何将其更改为默认颜色或至少检测操作栏的主题,以便我可以创建if语句来更改文本的颜色。

enter image description here     

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <ImageView
            android:id="@+id/imagenotileft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher"/>

        <RelativeLayout
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp">
            <TextView
                android:id="@+id/title"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:hint="Title Text"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"/>

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/title"
                android:hint="Content Text"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>

        </RelativeLayout>

    <TextView
        android:id="@+id/space2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="        "
        android:layout_toLeftOf="@+id/right2"/>


        <RelativeLayout
            android:id="@+id/parentRa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            >


            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="12:12"
                android:text="12:12"
                />

            <ImageView
                android:id="@+id/imagenotiright"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_action_chat"
                android:layout_below="@+id/time"
                android:nestedScrollingEnabled="false" />

        </RelativeLayout>
        </LinearLayout>
</RelativeLayout>

CODE:

public void CustomNotification(String strtext) {
        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.customnotification);

        // Set Notification Title
        String strtitle = getString(R.string.customnotificationtitle);
        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        intent.putExtra("String T", "");
        //intent.putExtra("Integer C", 0);

        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // Set Icon
                .setSmallIcon(R.drawable.ic_launcher)
                        // Set Ticker Message
                .setTicker(getString(R.string.customnotificationticker))
                        // Dismiss Notification
                .setAutoCancel(true)
                        // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                        // Set RemoteViews into Notification
                .setContent(remoteViews);

        // Locate and set the Image into customnotificationtext.xml ImageViews
        remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
        remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);

        // Locate and set the Text into customnotificationtext.xml TextViews
        remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
        remoteViews.setTextViewText(R.id.text, strtext);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());

    }

我将不胜感激任何帮助。非常感谢你。

1 个答案:

答案 0 :(得分:1)

我不太确定,但您可以尝试这样的事情:

if (android.os.Build.VERSION.SDK_INT > 20) {
    // I am not sure of this method
    remoteViews.setTextColor(R.id.text, Color.WHITE);
}