Translucent status bar in Android

后端 未结 6 2043
猫巷女王i
猫巷女王i 2020-12-17 01:17

I am using the following layout (main_activity.xml)




        
相关标签:
6条回答
  • 2020-12-17 01:53
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Window window = getWindow();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(Color.TRANSPARENT); } }
    
    }
    

    Use this in your activity. But changing the status bar color is only allowed after android L

    0 讨论(0)
  • 2020-12-17 02:00

    Add these two lines in the onCreate of the activity that you want its layout status bar to be transparent

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
    

    No need to change your app theme or xml layout. In doing this your bar doesn't have to be transparent throughout your whole app if you don't want to.

    0 讨论(0)
  • The code below is making the status bar transparent but navigation bar opaque for API21+.

    NOTE: you shouldn't be setting android:fitsSystemWindows="true".

    override fun onCreate(savedInstanceState: Bundle?) {
          setupContentWindow()
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
    }
    
    fun setupContentWindow() {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
    
    0 讨论(0)
  • 2020-12-17 02:10

    It seems some people have grey status bar instead of transparent. To avoid this problem do the next in your activity:

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        window.apply {
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            statusBarColor = Color.TRANSPARENT
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
    }
    
    0 讨论(0)
  • 2020-12-17 02:12

    try this:

    add this in your app theme:

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

    Set android:fitsSystemWindows=”true” to root container

    Now in your activity onCreate():

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    

    Updated, Result:

    you can make the snackbar draw on top of screen:

    Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
    View view = snack.getView();
    FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity = Gravity.TOP;
    view.setLayoutParams(params);
    snack.show();
    

    For more info see here

    0 讨论(0)
  • 2020-12-17 02:17

    Please remove this line from your CoordinatorLayout:

    android:fitsSystemWindows="true"
    

    Edited, Result:

    0 讨论(0)
提交回复
热议问题