【问题标题】:fitsSystemWindows on API16 device doesnt workAPI16 设备上的 fitSystemWindows 不起作用
【发布时间】:2016-07-16 16:19:52
【问题描述】:

我的活动请求全屏布局:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

并且在 xml 布局中将 fitsSystemWindows 属性设置为填充状态栏高度:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<!-- The main content view -->

        <android.support.design.widget.CoordinatorLayout
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                    app:contentScrim="@color/actionbar_title_color"
                    android:fitsSystemWindows="true">

使用appcompat-v7:23.2.1 编译,在 API21 设备上运行良好,但在 API16 设备上不应用填充。有什么提示吗?

更新

赏金:为什么在 API16 设备上没有应用填充?

【问题讨论】:

    标签: android android-support-library android-design-library android-support-design android-drawer


    【解决方案1】:

    来自官方文档Android development pattern

    在 KitKat 及以下上,您的自定义视图可以覆盖 fitSystemWindows() 并提供您想要的任何功能 — 只需 如果您已经使用了插图,则返回 true;如果您愿意,则返回 false 给其他意见一个机会。

    但是,在 Lollipop 和更高版本的设备上,我们提供了一些新的 API 使自定义此行为更加容易和一致 视图的其他行为。您将改为覆盖 onApplyWindowInsets(),它允许视图根据您的需要消耗尽可能多或尽可能少的插图,并且能够调用 dispatchApplyWindowInsets() 根据需要在子视图上。

    你甚至不需要子类化你的视图,如果你只 需要在 Lollipop 和更高版本上的自定义行为,您可以使用 ViewCompat.setOnApplyWindowInsetsListener(),会给出 优先于视图的onApplyWindowInsets()。 ViewCompat 也 提供调用onApplyWindowInsets() 的辅助方法和 dispatchApplyWindowInsets() 没有版本检查。

    现在,您需要检查使用哪个 api 级别的条件以及根据该应用条件,如

    if(API < Kitkat){
        fitSystemWindows()
    }else if(API > Lollipop){
        onApplyWindowInsets()
    }
    

    详情请查看this answer

    另请阅读http://developer.android.com/reference/android/view/View.html#fitSystemWindows%28android.graphics.Rect%29

    【讨论】:

    • 因此,在 API16 上我应该覆盖 fitSystemWindows() 吗?我不明白为什么,因为默认实现应该可以添加所需的填充。
    • 如果这不起作用,那么您需要应用条件,如果 api 是这个(KitKat 或以下),则该代码用于 API 级别棒棒糖的 else 代码
    • 谢谢,我知道所有这些帖子,但这里的问题是如何在 API 16 上工作,如文档所说的 'android:fitsSystemWindows="true"'。
    • 我已经告诉过你,你必须检查我的答案中写的条件,它肯定会起作用。
    • 抱歉,我在您的代码中没有看到任何解决方案:fitSystemWindows()onApplyWindowInsets() 将在自定义视图中被覆盖以设置其自己的行为,而不是调用。我没有自定义视图,我有一个 DrawerLayout,它应该满足填充状态栏高度的请求并将其传递给 CoordinatorLayout。
    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多