首页 > 系统 > Android > 正文

Android实现通知栏透明的方法

2020-04-11 11:03:49
字体:
来源:转载
供稿:网友

这个特性是andorid4.4支持的,最少要api19才可以使用,也就是说如果Android的机子是低于4.4,沉浸通知栏是没有效果的。下面介绍一下使用的方法,非常得简单。

 /**   * 设置通知栏 这个方法在onCreate()实现,如果是在父类的onCreate()中添加,即使所有继承了该父类都会有沉浸通知栏。   */public void initSystemBar() {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      setTranslucentStatus(true);      SystemBarTintManager tintManager = new SystemBarTintManager(this);      tintManager.setStatusBarTintEnabled(true);      tintManager.setStatusBarTintResource(R.color.red);    }  }  /**   * 设置通知栏的状态   * @param on   */  @SuppressLint("InlinedApi")   private void setTranslucentStatus(boolean on) {     Window win = this.getWindow();     WindowManager.LayoutParams winParams = win.getAttributes();     final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;     if (on) {       winParams.flags |= bits;     } else {       winParams.flags &= ~bits;     }     win.setAttributes(winParams);   }

在最后在布局文件中添加:android:fitsSystemWindows="true"

即可实现。

Android5.0全透明状态栏效果,具体实例代码如下所示:

实现上述效果的代码如下:

public class MainActivity extends Activity {  @SuppressLint("InlinedApi")  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    getWindow().requestFeature(Window.FEATURE_NO_TITLE);    if(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {      Window window = getWindow();      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS          | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN              | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION              | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);      window.setStatusBarColor(Color.TRANSPARENT);      window.setNavigationBarColor(Color.TRANSPARENT);    }    setContentView(R.layout.activity_main);  }}

以上代码写的不好,还请各位大侠多多提出,同时希望本文分享对大家有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表