How many WindowInsets are there?

前端 未结 2 937
迷失自我
迷失自我 2021-02-19 02:28

I do not understand about WindowInsets rects, because docs says that:

The system window inset represents the area of a full-screen window that is partiall

2条回答
  •  时光说笑
    2021-02-19 03:16

    WindowInsets describes a set of insets for window content. In other words, WindowInsets has one rect of the available area for the app (and has other info like isRound). Available area excludes the rect of StatusBar and NavigationBar.

    If you just want to know the height of StatusBar and NavigationBar, check this.

    You can get WindowInsets like following. Following example uses WindowInsetsCompat for compatibility.

    In your style.xml:

    
    

    In your AndroidManifest.xml

    
    
            ...
    
    
    

    In your layout xml: (fitsSystemWindows should be set to get WindowInsets.)

    
    
        
    
    
    

    In your Activity (or any place):

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            View container = findViewById(R.id.container);
    
            ViewCompat.setOnApplyWindowInsetsListener(container, new OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
    
                    //you can do something with insets.
                    int statusBar = insets.getSystemWindowInsetTop(); //this is height of statusbar
                    int navigationBar = insets.getStableInsetBottom(); //this is height of navigationbar
                    Log.d("MainActivity", String.format("%s %s", statusBar, navigationBar));
    
                    ViewCompat.onApplyWindowInsets(v, insets);
                    return insets;
                }
            });
        }
    }
    

    WindowInsets is like this:

提交回复
热议问题