我的应用程序中有一个屏幕,可以在透明状态栏后面加载图像。有时这些图像足够轻,以至于状态栏图标和文本会在图像中丢失。
如何根据下方图像的颜色将状态栏设置为亮/暗?我试图保持状态栏完全透明而不添加背景。
最佳答案
您可以使用此解决方案根据您的背景更改状态栏颜色:
// Set the background and text colors of a toolbar given a
// bitmap image to match
public void setToolbarColor(Bitmap bitmap) {
// Generate the palette and get the vibrant swatch
// See the createPaletteSync() method
// from the code snippet above
Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = p.getVibrantSwatch();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Load default colors
int backgroundColor = ContextCompat.getColor(getContext(),
R.color.default_title_background);
int textColor = ContextCompat.getColor(getContext(),
R.color.default_title_color);
// Check that the Vibrant swatch is available
if(vibrantSwatch != null){
backgroundColor = vibrantSwatch.getRgb();
textColor = vibrantSwatch.getTitleTextColor();
}
// Set the toolbar background and text colors
toolbar.setBackgroundColor(backgroundColor);
toolbar.setTitleTextColor(textColor);
}
现在,只需从您的背景生成位图,然后将其与此方法一起使用。欲了解更多信息,请访问官方开发者网站 - Color palette API
关于android - 基于图像的亮/暗状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70850812/