有多少个WindowInsets? [英] How many WindowInsets are there?

查看:450
本文介绍了有多少个WindowInsets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解WindowInsets rects,因为文档说:

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

系统窗口插图代表全屏窗口的区域,该区域被状态栏,导航栏,IME或其他系统窗口部分或完全遮盖.

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.

因此,可以有多个WindowInsets,每个都有其自己的rect(一个用于状态栏,另一个用于导航栏...),我如何检索它们?

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?

还是只有一个WindowInsets,其左上右下坐标是该应用程序可用窗口的矩形?

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

推荐答案

WindowInsets 描述了一组窗口内容的插图. 换句话说,WindowInsets具有该应用程序可用区域的一个区域(并具有其他信息,例如isRound).可用区域不包括StatusBarNavigationBar的矩形.

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.

如果您只想知道StatusBarNavigationBar的高度,请检查

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

您可以像下面这样获得WindowInsets. 以下示例使用 WindowInsetsCompat 来实现兼容性.

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

在您的style.xml中:

In your style.xml:

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

在您的AndroidManifest.xml中

In your AndroidManifest.xml

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

        ...

</application>

在布局xml中:(应该将fitsSystemWindows设置为获取WindowInsets.)

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是这样的:

WindowInsets is like this:

这篇关于有多少个WindowInsets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆