微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将状态栏背景设置为渐变颜色或android中的drawable

我想将状态栏背景设置为渐变主题,状态栏和动作栏颜色应该是相同的渐变可绘制,根据文档,我们可以使用API​​级别21及以上的状态栏将颜色设置为使用

<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这个库但仍然没有运气

先感谢您!!

解决方法:

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

感谢每一位人士的帮助

编辑

如果上面的代码不起作用,请尝试在styles.xml中添加

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐