使用ViewPager和fitsSystemWindow控制状态栏

问题描述 投票:1回答:3

我有一个ViewPagerFragments,其中包含全屏图像和一些底部对齐的控件。

我想确保控件不会在fitsSystemWindow的半透明导航栏后面消失,但我似乎无法使它工作......

当我在没有ViewPager的情况下使用相同的xml时它确实有效,所以看起来ViewPager“打破”使用fitsSystemWindow的能力。

我为活动编写的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

这就是我对片段的看法:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#000"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/initial_background_image"
        android:scaleType="centerCrop"/>

    <RelativeLayout
        android:id="@+id/buttons_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/button_icon_offset"
        android:layout_marginBottom="@dimen/button_icon_offset"
        android:layout_marginRight="@dimen/button_icon_offset"
        android:fitsSystemWindows="true"
        android:layout_gravity="bottom">

        <Button
            android:id="@+id/info_button"
            style="@style/button_icon"
            android:layout_gravity="left|bottom"
            android:background="@drawable/button_info"
            android:textAlignment="center"/>

        <Button
            android:id="@+id/share_button"
            style="@style/button_icon"
            android:layout_alignTop="@id/info_button"
            android:layout_marginRight="@dimen/button_icon_divider"
            android:layout_toLeftOf="@+id/like_button"
            android:background="@drawable/button_share"/>

        <Button
            android:id="@+id/like_button"
            style="@style/button_icon"
            android:layout_alignParentRight="true"
            android:background="@drawable/button_like"/>
    </RelativeLayout>
</FrameLayout>

所以这就是我最终的结果...但我希望按钮出现在导航栏上方。

This is what I dont want

android android-layout android-fragments android-viewpager android-view
3个回答
0
投票

首先,请参阅有关thiss的WindowInset答案。然后你会明白,你的根布局 - FrameLayout,不会将WindowInsets发送给ViewPager

使用ViewCompat.setOnApplyWindowInsetsListener() API将WindowInsets发送到ViewPager并使你的ViewPager适合窗口插入,即将android:fitsSystemWindows="true"应用于ViewPager和它的父级。


0
投票

只需在style.xml中更改/删除它即可

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...    
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    ...
</style>

如果仍然不工作?你正在使用NestedScrollView然后添加“android:clipToPadding”

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/light_green"
            android:clipToPadding="false" <--add this
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

0
投票

您可以在按钮底部添加边距,将它们放在导航栏上方。

android:layout_marginBottom="?attr/actionBarSize"
© www.soinside.com 2019 - 2024. All rights reserved.