简体   繁体   English

单击时更改自定义通知中图像按钮的背景颜色

[英]change background color of image button in custom notification when clicked

I have a custom notification layout that contains four image buttons. 我有一个包含四个图像按钮的自定义通知布局。

I need to change the background color of the image buttons when user click on them. 用户单击它们时,我需要更改图像按钮的背景颜色。

I need to use 我需要用

ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
imgButton.setBackgroundColor(Color.BLUE);

but this cause a nullpointerexception error because compiler can't find the recentAppButt image button because these image buttons are in the custom notification layout. 但这会引起nullpointerexception错误,因为编译器找不到自定义通知布局中的最近使用的图像按钮,因为这些图像按钮在自定义通知布局中。

this is some code from the mainActivity.class in which i create or remove the notification 这是来自mainActivity.class的一些代码,我在其中创建或删除通知

private void createNotificationIcons() {
    String ns = Context.NOTIFICATION_SERVICE;
    notificationManager = (NotificationManager) getSystemService(ns);

    notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.home)
            .setContentTitle("My notification")
            .setContentText("All Buttons!")
            .setOngoing(true) /** notification will appear as ongoing notification*/
            .build();                      
    /** set a custom layout to the notification in notification drawer  */
    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification);
    notification.contentView = notificationView;

    Intent recentAppIntent = new Intent(Context.NOTIFICATION_SERVICE);
    recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle recentAppBundle = new Bundle();   
    recentAppBundle.putInt("userAnswer", 1);
    recentAppIntent.putExtras(recentAppBundle);
    PendingIntent pendingrecentAppIntent = PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, 0);
    notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);

    notificationManager.notify(1, notification);
}


public class SwitchButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle answerBundle = intent.getExtras();
        int userAnswer = answerBundle.getInt("userAnswer");
        if(userAnswer == 1) {
            ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
            imgButton.setBackgroundColor(Color.BLUE);
            Toast.makeText(context, "Recent Apps", Toast.LENGTH_SHORT).show();
            Log.d("Recent", "recent app butt clicked");
            openRecentApps();
        }
    }
}

public class BackgroundService extends Service {

    MainActivity mainActivity = new MainActivity();
    MainActivity.SwitchButtonListener switchButtonListener = mainActivity.new   SwitchButtonListener();

    /** Called when the service is being created. */
    @Override
    public void onCreate() {
        super.onCreate();
    }

    /** The service is starting, due to a call to startService() */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(switchButtonListener, new     IntentFilter(Context.NOTIFICATION_SERVICE));
    return START_STICKY;
}

    /** Called when The service is no longer used and is being destroyed */
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(switchButtonListener);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

and this the notification.xml 这是notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >

<!-- create home button in notification drawer -->

<ImageButton
    android:id="@+id/homeButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:layout_marginLeft="1dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/discription"
    android:src="@drawable/home" />

<!-- create recent apps button in notification drawer -->

<ImageButton
    android:id="@+id/recentAppButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/discription"
    android:src="@drawable/recent" />

<!-- create lock screen button in notification drawer -->

<ImageButton
    android:id="@+id/lockScreenButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/discription"
    android:src="@drawable/lock" />

<!-- create allButtons button in notification drawer -->

<ImageButton
    android:id="@+id/allButtonsButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:background="#FF990000"
    android:contentDescription="@string/discription"
    android:src="@drawable/all_buttons" />

Thanks in advance. 提前致谢。

Check if in your layout file, your ImageView has parameters for height and width which are set to wrap_parent . 检查布局文件中的ImageView是否具有将height和width的参数设置为wrap_parent

if any of those (layout_height or layout_width) are set to wrap_content , and you have don't have an actual image set on the ImageView, it's height is set to 0. In other words, you should change the wrap_content to another value such as 100dp for example and see if the color changes. 如果其中任何一个(layout_height或layout_width)设置为wrap_content ,并且您在ImageView上没有设置实际图像,则将其高度设置为0。换句话说,您应该将wrap_content更改为另一个值,例如以100dp为例,看看颜色是否改变。

findViewById is a method of the Activity class and will not work on RemoteViews and notifications. findViewByIdActivity类的一种方法,不适用于RemoteViews和通知。 You are getting a null point exception because R.id.recentAppButt doesn't exist in your activity view. 您收到空值异常,因为R.id.recentAppButt在您的活动视图中不存在。

You can achieve what you want by creating two icons with different background colours in your drawables folder. 您可以通过在drawables文件夹中创建两个具有不同背景颜色的图标来实现所需的功能。 You can then switch between the two icons with the setImageViewResource method of the RemoteViews class as follows 然后,可以使用RemoteViews类的setImageViewResource方法在两个图标之间切换,如下所示

notificationView.setImageViewResource(R.id.recentAppButt, R.drawable.icon_with_blue_background)

You then need to rebuild your notification and re- notify it through the NotificationManager so that the background colour is updated. 然后,您需要重建通知并通过NotificationManager重新通知 ,以便更新背景色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM