【问题标题】:Android StatuBar overlayAndroid 状态栏覆盖
【发布时间】:2015-12-02 08:58:09
【问题描述】:

我开发了一个简单的 Android 应用程序来阅读 PDF 书籍。我大约一年前开始开发,所以我的目标是 SDK 版本 18。现在需要开始使用 Material Design,所以我将我的 targetSdkVersion 设置为 22。 在使用工具栏进行一些自定义后,我遇到了全屏模式的问题。 在第一张照片中,一切都很好。全屏模式未激活。

在第二张图片中,您可以在屏幕顶部看到一个白行。这很难看,我需要删除它。

当我针对 SDK 版本 18 时,这很好,因为我已经设置,StatusBar 将覆盖整个 Activity 的内容。现在好像不行了。

在我的活动中,我有以下几行:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = getWindow();                
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);

    // some other code is here...        
}

全屏模式切换代码如下:

protected void fullscreenOn() {
    if (mActivity != null && !mSearchModeOn) {
        Window window = mActivity.getWindow();
        View decorView = window.getDecorView();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View
                    .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View
                    .SYSTEM_UI_FLAG_IMMERSIVE);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            mActionBar.hide();
        }

        bottomBarsLayout.setVisibility(View.GONE);
        mIsFullscreenOn = true;
    }
}

protected void fullscreenOff() {
    if (mActivity != null) {
        Window window = mActivity.getWindow();
        View decorView = window.getDecorView();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            decorView.setSystemUiVisibility(mNormalUIVisibility);
        } else {
            window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            mActionBar.show();
        }

        bottomBarsLayout.setVisibility(View.VISIBLE);
        mIsFullscreenOn = false;
    }
}

几乎一切正常。这里只有一个问题——屏幕顶部的白行问题。 你能帮我吗,我怎样才能让状态栏覆盖?这意味着,活动的内容将覆盖整个屏幕。 我知道我可以使用android:colorPrimaryDark 更改 StatusBar 的颜色,但我不想这样做。

更新

请注意,我的 Activity 扩展了 AppCompatActivity

更新 2

我的活动布局如下:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include
            layout="@layout/toolbar"/>

        <include layout="@layout/fragment_single_root"/>

    </RelativeLayout>

    <include layout="@layout/menu_list"/>

</android.support.v4.widget.DrawerLayout>

工具栏在这里:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
/>

附加到活动的片段的布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.radaee.reader.PDFReader
    android:id="@+id/pdf_reader_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

<RelativeLayout
    android:id="@+id/read_book_bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="?attr/bottomBarBackground"
    android:layout_alignParentBottom="true"
    android:visibility="gone">

    <Button
        android:id="@+id/img_view_read_book_content"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_margin="5dp"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/title_content"
        android:textSize="12sp"
        android:background="?attr/imgBtnContent" />

    <SeekBar
        android:id="@+id/seek_bar_read_book_listing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/text_view_read_book_pages"
        android:layout_toRightOf="@id/img_view_read_book_content"
        android:gravity="center" />

    <TextView
        android:id="@id/text_view_read_book_pages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="?attr/simpleFontColor"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/read_book_search_bar"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:fitsSystemWindows="true"
    android:layout_alignParentBottom="true"
    android:background="?attr/bottomBarBackground"
    android:orientation="horizontal"
    android:padding="10dp"
    android:visibility="gone"
    android:weightSum="5">

    <ImageView
        android:id="@+id/img_view_search_prev"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="?attr/imgPrevSearchResult" />

    <TextView
        android:id="@+id/text_view_search_info"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="3"
        android:textColor="?attr/simpleFontColor"
        android:gravity="center"
        android:singleLine="false"
        android:text="Test" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pdf_search_progress_bar"
        android:layout_gravity="center"
        android:indeterminate="true"
        android:layout_weight="3"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/img_view_search_next"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="?attr/imgNextSearchResult" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/relative_layout_custom_preview_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone">

    <cz.project.base.view.TwoWayView
        android:id="@+id/two_way_view_page_preview"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:drawSelectorOnTop="true" />
</RelativeLayout>

【问题讨论】:

    标签: android overlay material-design statusbar


    【解决方案1】:

    设置android:fitsSystemWindows="false",这将避免在顶部和底部填充。

    【讨论】:

    • 好的,谢谢。但是现在操作栏被状态栏覆盖了。兄弟,我该如何解决?
    【解决方案2】:

    移动

    Window window = getWindow();                
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
    

    之前

    super.onCreate(savedInstanceState);
    

    或者你可以使用这个:

    this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Window w = getWindow(); // in Activity's onCreate() for instance
                w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    super.onCreate(savedInstanceState);
    

    用于半透明导航和系统栏,工具栏颜色相同。

    【讨论】:

    • 谢谢。我试过了,但它不起作用。我看到了完全相同的行为。
    • 抱歉,我还是看不出有什么不同。我看到了同样的行为。
    • 你能发布 XML 布局吗?
    • 当然。您想查看 Activity 的布局、附加阅读片段的位置,还是这个附加片段的布局?
    【解决方案3】:

    使用此样式并将其设置为您的活动 -

    <style name="FullScreen" parent="@style/Theme.AppCompat.Light">
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowContentOverlay">@null</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2013-09-26
      相关资源
      最近更新 更多