当键盘弹出时,状态栏会显示。

11

我使用CollapsingToolbarLayout组件,并成功地移除了StatusBar,方法如下:

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
问题在于当软键盘弹出时,状态栏再次出现,占用了宝贵的空间。
如何在ActivityView上永久隐藏它?
如果我将其设置为FullScreen,它可以工作,但我失去了SOFT_INPUT_ADJUST_RESIZE参数。主窗口在软键盘出现时滚动,而不是按需要调整大小。
更新:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/my_appar"
        android:fitsSystemWindows="false"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/white">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:toolbarId="@+id/collapsetoolbar"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"
            app:contentScrim="?attr/colorPrimary">

            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:elevation="4dp"
                android:layout_height="wrap_content"
                android:layout_width="match_parent">
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="... .MyActivity"
        android:orientation="vertical"
        >

        <FrameLayout
            android:id="@+id/yt_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <fragment
            ...
        </FrameLayout>

        <com.byte_artisan.mchat.compoundViews.chatview.ChatView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/fragment1"
            android:id="@+id/chat_view_id">
        </com.byte_artisan.mchat.compoundViews.chatview.ChatView>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

代码:

 protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mchat);

     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE );
..
}


@Override
protected void onResume() {
    super.onResume();

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
}
该活动正在使用在清单文件中声明的NoActionBar主题。

发布你的布局代码。 - Jayanth
2个回答

7
在你的样式(activity style)中添加这行代码。
<item name="android:windowFullscreen">true</item>

除非您使用ScrollView包装活动内容,否则无法在全屏活动中使用SOFT_INPUT_ADJUST_RESIZE

为使其正常工作,您需要将布局内容包装在ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- everything you already have -->

</LinearLayout>

请告诉我它是否有效。


可以工作,但会失去SOFT_INPUT_ADJUST_RESIZE参数。 - MiguelSlv
我没有使用ScroolView。我将发布布局。 - MiguelSlv
不,只是继续滚动。设置全屏不应该影响窗口行为,但它确实会影响。 - MiguelSlv
查找支持此行为的文档。请访问 https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#SOFT_INPUT_ADJUST_RESIZE。无论如何,谢谢。 - MiguelSlv
我会调查一下,谢谢 :) - Jayanth

2

需要注意的几点:

1)一旦UI标志已清除(例如通过导航离开活动),如果您想再次隐藏栏,则您的应用程序需要重置它们。请参见响应UI可见性变化以了解如何监听UI可见性变化,使您的应用程序能够相应地做出反应。

2)您设置UI标志的位置很重要。如果您在活动的onCreate()方法中隐藏系统栏,并且用户按Home键,则系统栏将重新出现。当用户重新打开活动时,onCreate()不会被调用,因此系统栏将保持可见状态。如果您希望系统UI更改在用户进入和退出活动时仍然存在,请在onResume()或onWindowFocusChanged()中设置UI标志。

3)仅当从中调用setSystemUiVisibility()的视图可见时,它才会生效。

4)从视图导航离开会导致使用setSystemUiVisibility()设置的标志被清除。

您应该在创建时添加此代码

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
// again hide it
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接