2016-11-16 83 views
1

我一直在嘗試構建自定義通知。Android自定義通知爲空

不幸的是,通知的理由是空的。我不明白這是怎麼可能的,沒有任何錯誤,通知只是2個圖標,但沒有文字。

我已經嘗試了API 23和24,兩者都是相同的東西。下面是結果的打印屏幕:

enter image description here

這裏是我的代碼:

MainActivity.java

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     showPreviousView(); 
    } 
} 

public void customNotification() { 
     RemoteViews remoteViews = new RemoteViews(getPackageName(), 
       R.layout.custom_notification); 

     String strtitle = getString(R.string.customnotificationtitle); 
     String strtext = getString(R.string.customnotificationtext); 

     Intent intent = new Intent(this, MainActivity.class); 
     intent.putExtra("title", strtitle); 
     intent.putExtra("text", strtext); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_play) 
       .setTicker(getString(R.string.customnotificationticker)) 
       .setAutoCancel(true) 
       .setContentIntent(pIntent) 
       .setContent(remoteViews); 

     remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_menu_camera); 
     remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.ic_menu_gallery); 

     remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle)); 
     remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext)); 

     NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationmanager.notify(0, builder.build()); 
    } 

String.xml

<resources> 
    <string name="app_name">The Music Player</string> 
    <string name="navigation_drawer_open">Open navigation drawer</string> 
    <string name="navigation_drawer_close">Close navigation drawer</string> 
    <string name="action_settings">Settings</string> 
    <string name="title_activity_browse">Browser</string> 

    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="notificationtitle">Android Notification Tutorial</string> 
    <string name="notificationtext">Notification Text</string> 
    <string name="notificationticker">Notification Ticker</string> 
    <string name="customnotificationtitle">Android Notification Tutorial</string> 
    <string name="customnotificationtext">Custom Notification Text</string> 
    <string name="customnotificationticker">Custom Notification Ticker</string> 
    <string name="lbltitle">"Title : "</string> 
    <string name="lbltext">"Text : "</string> 
    <string name="notification">Notification</string> 
    <string name="customnotification">Custom Notification</string> 
</resources> 

custom_notification.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imagenotileft" 
     android:text="dayme son"/> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/title" 
     android:layout_toRightOf="@+id/imagenotileft" /> 

    <ImageView 
     android:id="@+id/imagenotiright" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:padding="10dp" /> 

</RelativeLayout> 

EDIT

MainActivity.java

public void customNotification() { 
     RemoteViews remoteViews = new RemoteViews(getPackageName(), 
       R.layout.custom_notification); 

     Intent intent = new Intent(this, MainActivity.class); 
     intent.putExtra("title", "title"); 
     intent.putExtra("text", "txt"); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_play) 
       .setTicker(getString(R.string.customnotificationticker)) 
       .setAutoCancel(true) 
       .setContentIntent(pIntent) 
       .setContent(remoteViews); 


     remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_menu_camera); 
     remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.ic_menu_gallery); 

     NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationmanager.notify(0, builder.build()); 
    } 

custom_notification

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

<TextView 
    android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/imagenotileft" 
    android:text="same text" 
    android:textColor="#00FFFF"/> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/title" 
    android:layout_toRightOf="@+id/imagenotileft" 
    android:text="this ia text" 
    android:textColor="#00FFFF"/> 

<ImageView 
    android:id="@+id/imagenotiright" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:padding="10dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="this is a try" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/text" 
    android:layout_toEndOf="@+id/text" 
    android:layout_marginLeft="17dp" 
    android:layout_marginStart="17dp" /> 

</RelativeLayout> 

結果

enter image description here

回答

4

通知文本不是空的,它只是白色的,所以你不要看它:)

下面是截圖帶着幾分飽和度改變/曝光/對比度,請嘗試從不同角度查看屏幕。

Image

上面清清楚楚地寫着:

Android Notification Tutorial 
Custom Notification Text 

嘗試改變字體顏色,例如。把你的xml:

android:textColor="@android:color/black" 
+3

哇,強烈的眼睛;) –

+0

我edtied我的帖子和我的代碼,但我仍然得到的結果。我試圖改變文本的顏色並添加一個按鈕。 –

+0

其實現在我可以看到按鈕,但不是文本... –