-->

How do I change the color of this bar when switchi

2019-08-14 18:29发布

问题:

With the addition of Lollipop, it appears you can now change the color of this window when changing apps. I don't know what it is called and therefore can't find any info on it but if you look at the image you can see that the Keep app is now yellow.

How do I go about changing this color?

Here is a link to the image, it won't let me attach it since I'm new

Thanks

回答1:

You can use setTaskDescription() to achieve this:

setTaskDescription(new ActivityManager.TaskDescription(label, icon, color));

For android documentation:

Sets information describing the task with this activity for presentation inside the Recents System UI. When getRecentTasks(int, int) is called, the activities of each task are traversed in order from the topmost activity to the bottommost. The traversal continues for each property until a suitable value is found. For each task the taskDescription will be returned in ActivityManager.TaskDescription.

Parameters taskDescription The TaskDescription properties that describe the task with this activity

https://developer.android.com/reference/android/app/Activity.html#setTaskDescription(android.app.ActivityManager.TaskDescription)



回答2:

1.normal way

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        String title = getString(R.string.app_name);
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        int color = getResources().getColor(R.color.color_primary);
        setTaskDescription(new ActivityManager.TaskDescription(title, icon, color));
}

2.reflected

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   try {
            Class<?> clazz = Class.forName("android.app.ActivityManager$TaskDescription");
            Constructor<?> cons = clazz.getConstructor(String.class, Bitmap.class, int.class);
            Object taskDescription = cons.newInstance(title, icon, color);

            Method method = ((Object) BaseActivity.this).getClass().getMethod("setTaskDescription", clazz);
            method.invoke(this, taskDescription); 
            } catch (Exception e) {

            }
}


回答3:

Simply put this code into the onCreate method of your target activity:

int color = getResources().getColor(R.color.your_top_bar_color);
setTaskDescription(new ActivityManager.TaskDescription(null, null, color));

Please be aware that the code above requires API level 21 (Android 5.0 Lolipop) or higher. In case you need to support older devices as well, you can surround the code with following condition:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    int color = getResources().getColor(R.color.your_top_bar_color);
    setTaskDescription(new ActivityManager.TaskDescription(null, null, color));
}

Also be aware that you will need to set the colour of top bar in every activity, otherwise your color will be reset when launching another activity. (You can ease this problem by putting the code into some kind of BaseActivity, from which other Activities will inherit.)

Helpful article about this topic: https://www.bignerdranch.com/blog/polishing-your-Android-overview-screen-entry/