-->

如何切换应用程序时,我改变这一栏的颜色?(How do I change the color of

2019-10-22 09:36发布

通过增加棒棒糖,看来你现在可以改变应用程序的时候改变这个窗口的颜色。 我不知道它叫什么,因此不能找到任何信息,但如果你看一下图片,你可以看到Keep应用现在是黄色。

我该如何去改变这个颜色?

这里是将图像的链接,它不会让我重视它,因为我是新

谢谢

Answer 1:

您可以使用setTaskDescription()来实现这一目标:

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

对于Android文档:

设置描述这个活动的最近通话系统界面内呈现的任务信息。 当getRecentTasks(INT,INT)被调用时,每个任务的活动,以便从至最低的最上面的活动走过。 直到一个合适的值被发现遍历持续每个属性。 每个任务的taskDescription将在ActivityManager.TaskDescription返回。

参数taskDescription描述任务与本次活动的TaskDescription性质

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



Answer 2:

1.normal方式

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) {

            }
}


Answer 3:

简单地把这个代码进入onCreate目标活动的方法:

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

请注意,上面的代码需要API级21(的Android 5.0棒棒糖)或更高。 如果你需要支持旧设备,以及,你可以围绕与以下条件的代码:

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));
}

另外要注意,你将需要设置每一项活动最上面一栏的颜色,否则你的颜色会启动另一个活动时被重置。 (您可以通过将代码置于某种BaseActivity,从中等活动将继承缓解这个问题。)

有关此主题的有用的文章: https://www.bignerdranch.com/blog/polishing-your-Android-overview-screen-entry/



文章来源: How do I change the color of this bar when switching apps?