android状态栏样式修改

举报
再见孙悟空_ 发表于 2022/01/14 00:09:16 2022/01/14
【摘要】   设置状态栏背景效果 package com.cy.translucentparent; import android.app.Activity;import android.os.Build;import android.view.View;import android.view.Window;import...

 

设置状态栏背景效果


  
  1. package com.cy.translucentparent;
  2. import android.app.Activity;
  3. import android.os.Build;
  4. import android.view.View;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. /**
  8. * Created by lenovo on 2017/4/25.
  9. */
  10. public class StatusNavUtils {
  11. /**
  12. * 状态栏透明去阴影(5.0以上)
  13. *
  14. * @param activity
  15. * @param color
  16. */
  17. public static void setStatusBarColor(Activity activity, int color) {
  18. Window window = activity.getWindow();
  19. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  20. window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  21. window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  22. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  23. window.setStatusBarColor(color);
  24. return;
  25. }
  26. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  27. window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  28. }
  29. }
  30. /**
  31. * 导航栏全透明去阴影(5.0以上)
  32. *
  33. * @param activity
  34. * @param color
  35. */
  36. public static void setNavigationBarColor(Activity activity, int color) {
  37. Window window = activity.getWindow();
  38. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  39. window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  40. window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  41. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  42. window.setNavigationBarColor(color);
  43. return;
  44. }
  45. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  46. window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  47. }
  48. }
  49. /**
  50. * 状态栏、导航栏全透明去阴影(5.0以上)
  51. *
  52. * @param activity
  53. * @param color_status
  54. * @param color_nav
  55. */
  56. public static void setStatusNavBarColor(Activity activity, int color_status, int color_nav) {
  57. Window window = activity.getWindow();
  58. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  59. window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  60. window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  61. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  62. window.setStatusBarColor(color_status);
  63. window.setNavigationBarColor(color_nav);
  64. return;
  65. }
  66. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  67. window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  68. window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  69. }
  70. }
  71. }

activity


  
  1. package com.cy.statusnavigationtrans;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import com.cy.translucentparent.StatusNavUtils;
  5. public class Status5Activity extends BaseActivity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_status5);
  10. StatusNavUtils.setStatusBarColor(this,0x33000000);
  11. }
  12. @Override
  13. public void onClick(View v) {
  14. }
  15. }

xml


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. style="@style/transparent_statusbar_fit"
  7. android:background="@color/colorPrimary"
  8. android:orientation="vertical">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="48dp"
  12. android:background="@color/colorPrimary">
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:gravity="center"
  17. android:text="StatusBar全透明(fitsystemwindows)"
  18. android:textColor="#ffffff" />
  19. </RelativeLayout>
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:background="@color/white"
  24. android:orientation="vertical"></LinearLayout>
  25. </LinearLayout>

style


  
  1. <resources>
  2. <style name="transparent_statusbar_fit">
  3. <item name="android:fitsSystemWindows">true</item>
  4. <item name="android:clipToPadding">false</item>
  5. </style>
  6. </resources>

DEMO下载

这样就可以了,谢谢 。希望可以帮助大家,欢迎加入我们的qq群:开发一群:454430053开发二群:537532956

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/51656695

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。