How many WindowInsets are there?

风格不统一 提交于 2019-12-09 14:18:12

问题


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 partially or fully obscured by the status bar, navigation bar, IME or other system windows.

So, multiple WindowInsets can be there each with its own rect (one for status bar, another for Navigation Bar ...), and how can I retrieve them?

Or is there only one WindowInsets and its left-top-right-bottom coordinates are the rect of the available window for the app?


回答1:


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:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    ...
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

In your AndroidManifest.xml

<application
        ...
        android:theme="@style/AppTheme">

        ...

</application>

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

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</FrameLayout>

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:




回答2:


There is only one type of WindowInsets which describes a set of insets for window content. since it is immutable and may be expanded to include more inset types in the future. you can create more instance of it. and you can also get the left,right etc insets of WindowInsets in pixels with methods like getStableInsetBottom() etc.



来源:https://stackoverflow.com/questions/38727257/how-many-windowinsets-are-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!