如何在Android中将状态栏背景设置为渐变颜色或可绘制对象 [英] how to set status bar background as gradient color or a drawable in android

查看:725
本文介绍了如何在Android中将状态栏背景设置为渐变颜色或可绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将状态栏背景设置为渐变主题,并且状态栏和操作栏颜色应具有相同的渐变可绘制度,根据文档,我们可以使用

I want to set status bar background as gradient theme also status bar and action bar color should same gradient drawable, as per documentation we can set color to status bar in API level 21 and above by using

<item name="android:statusBarColor">@color/colorPrimary</item>

但是我正在搜索类似

<item name="android:statusBarDrawable">@drawable/myDrawable</item>

我看过使用

 <item name="android:windowTranslucentStatus">false</item>
   <item name="android:windowTranslucentNavigation">false</item>

,但在这种情况下,状态栏和操作栏重叠(使用fitSystemWindow = true,但仍未解决),也可以尝试使用 https ://github.com/jgilfelt/SystemBarTint 这个库,但是仍然没有运气

but in that case status bar and action bar overlap (use fitSystemWindow =true but still not solved) also try with https://github.com/jgilfelt/SystemBarTint this library but still no luck

提前谢谢!

推荐答案

对于某些想要将渐变颜色设置为状态栏背景的人,您可以在活动之前使用以下方法setContentView()

For some one who want to set gradient color to status bar background you can use following method in your activity before 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);
        }
    } 

感谢大家的帮助

编辑

如果上面的代码不起作用,请尝试将其添加到您的styles.xml中:

If the above code don't work, try to add this in your styles.xml:

<style name="AppTheme.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

这篇关于如何在Android中将状态栏背景设置为渐变颜色或可绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆