android - 如何设置与操作栏相同的渐变可绘制状态栏?

标签 android gradient drawable statusbar

我尝试了很多方法,但它与工具栏和状态栏重叠。它还返回按底部导航默认值。 我添加了以下代码-

 @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  Window w = getWindow();  w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);//allow window to extend outside of the screen.
            w.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);// override FLAG_FULLSCREEN and force the screen decorations (such as the status bar) to be shown.         w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR);            w.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);           w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        super.onCreate(savedInstanceState);

    }

enter image description here

最佳答案

在setContentView之前调用此方法。

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setStatusBarGradiant(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = activity.getWindow();
        Drawable background = activity.getResources().getDrawable(R.drawable.gradient_theme);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
        window.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));
        window.setBackgroundDrawable(background);
    }
} 

还要确保您使用的主题 AppTheme.NoActionBar。检查this .如果它不起作用,请检查问题的其他答案。

隐藏底部导航栏使用这个

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

检查 this了解更多信息。

关于android - 如何设置与操作栏相同的渐变可绘制状态栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455770/

相关文章:

android - 如何将渐变可绘制设置为 FloatingActionButton 上的 backgroundTint 属性

android - 在 android 4.4 上加载 Assets 文件时未捕获 ReferenceError

android - 手机间隙 : Add android platform to an already built app

r - 如何在R中获得abline的梯度

opencv - 从索贝尔确定图像梯度方向?

android - 在自定义 View 中为 Drawable 设置动画

android - 如何解决 Cannot find referenced class warnings when building for Amazon?

android - 在 RecyclerView Adapter 的 onBindViewHolder 中获取上下文

iphone - 尝试使用 CAGradientLayer 时出现编译错误

android - 在圆圈内绘制文字